 <p>You can set option async of ajax to false to wait for the reply from server. Therefore, the code may be like the below.</p> <pre><code>function checkAnswer(question, answer){ result = false; jQuery.ajax({ async: false, url: window.base_url+'ajax/check_answer/'+question+'/'+answer+'/', success: function(data){ result = data.success if (!data.success){ jQuery('.question',base).html(data.message); } } }); return result; } </code></pre>