Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>jConfirm('are you sure?', 'Confirmation Dialog', function(r) { result = r; response = true; return r; } ); if (response == true) { </code></pre> <p>This betrays a misunderstanding of the sequence of events that occurs using asynchronous code. Just because you've written it inline doesn't mean it's going to execute strictly top-to-bottom.</p> <ol> <li>jConfirm is called, receiving a function as one of its parameters, which it remembers.</li> <li>jConfirm displays its UI on the page and returns immediately.</li> <li>The 'if (response==true)' line executes. Really this should just read 'if (response)', the boolean comparison is superfluous. But in any case response is of course false. Your function gives up and exits, giving control back to the browser.</li> <li>The user clicks jConfirm's UI.</li> <li>jConfirm only now jumps into action and calls back the function you gave it and it remembered earlier.</li> <li>Your nested function sets response true, far too late for the 'if (response==true)' condition to do anything with it.</li> </ol> <p>You have written "//wait for response" as an alternative, but there is no JavaScript code you can write that will actually do that. Your function must return to give control back to the browser, before the browser can fire the click events on the jConfirm UI that make processing proceed.</p> <p>Ways to make asynchronous code work in a synchronous context (and vice versa) exist - in particular threads and coroutines (and their limited relation generators). But JavaScript has none of these features, so you must write your code to fit the synchronous-or-asynchronous model your library is using.</p>
 

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