Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you lack some understanding of the concept of ajax. it's called "asynchronous" for a reason.<br> if you remove the <code>async: false</code> (and you should, otherwise it's not really ajax), then the request for 'checkuser.php' is done asynchronously. meaning that the script continues it's execution, and in the meanwhile, in the "background", the ajax request is made.<br> when it is completed, then the callback function of 'success' is executed (whenever it happens you can't be certain).<br> therefore, if you want something to happen only after the ajax requested is finished successfully, then you should put it in the 'success' function.</p> <p>in case you do remove the <code>async: false</code>, then the order of execution is as follows: </p> <ol> <li>call to function 'checkUsername'</li> <li>assign 'ret=false'</li> <li>invoke ajax request (not waiting for it to finish)</li> <li>returning 'ret'</li> <li>ajax finished it's execution and executes 'success()'</li> <li>assign 'ret=true', but at this point 'ret' no longer exists.</li> </ol> <p>to understand the difference between sync and async, execute the following two examples:</p> <p><strong>example 1:</strong> </p> <pre><code>$.ajax({ type: 'POST', url: 'checkuser.php', async: false, data: {'username':username}, success: function(data){ alert('1'); } }); alert('2'); </code></pre> <p><strong>example 2:</strong> </p> <pre><code>$.ajax({ type: 'POST', url: 'checkuser.php', data: {'username':username}, success: function(data){ alert('1'); } }); alert('2'); </code></pre> <p>in 'example 1' you'll be alerted with '1' and then with '2'.<br> but in 'example 2' you'll be alerted first with '2' and then with '1'.<br> That's because in 'example 1' the script waits for the execution of the ajax request to complete before continuing, but in 'example 2' the script leaves the ajax request running in the "background", and in the meantime continuing with the rest of the script.</p> <p>hope this makes it clear.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload