Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There aren't two copies of the variable lying around. Javascript in web browsers is <em>single threaded</em> (unless you use the <a href="http://www.w3.org/TR/workers/" rel="nofollow noreferrer">new web workers stuff</a>). So the anonymous function never has the chance to run, because <code>Wait</code> is tying up the interpreter.</p> <p>You can't use a busy-wait functions in browser-based Javascript; nothing else will ever happen (and they're a bad idea in most other environments, even where they're possible). You have to use callbacks instead. Here's a minimalist reworking of that:</p> <pre><code>var interval_id; var countdowntimer = 0; function Wait(wait_interval, callback) { countdowntimer = wait_interval; interval_id = setInterval(function() { if (--countdowntimer &lt;=0) { clearInterval(interval_id); interval_id = 0; callback(); } }, 1000); } // Wait a bit: 5 secs Wait(5, function() { alert("Done waiting"); }); // Any code here happens immediately, it doesn't wait for the callback </code></pre> <p><strong>Edit</strong> Answering your follow-up:</p> <blockquote> <p>But, at which point in the processing of this single thread, does the so called asynchronous call using setInterval actually happen? Is it just between function calls? Surely not, what about functions that take a long time to execute?</p> </blockquote> <p>Pretty much, yeah&nbsp;&mdash; and so it's important that functions not be long-running. (Technically it's not even between function calls, in that if you have a function that calls three other functions, the interpreter can't do anything else while that (outer) function is running.) The interpreter essentially maintains a queue of functions it needs to execute. It starts starts by executing any global code (rather like a big function call). Then, when things happen (user input events, the time to call a callback scheduled via <code>setTimeout</code> is reached, etc.), the interpreter pushes the calls it needs to make onto the queue. It always processes the call at the front of the queue, and so things can stack up (like your <code>setInterval</code> calls, although <code>setInterval</code> is a <em>bit</em> special&nbsp;&mdash; it won't queue a subsequent callback if a previous one is still sitting in the queue waiting to be processed). So think in terms of when your code gets control and when it releases control (e.g., by returning). The interpreter can <em>only</em> do other things after you release control and before it gives it back to you again. And again, on some browsers (IE, for instance), that same thread is also used for painting the UI and such, so DOM insertions (for instance) won't show up until you release control back to the browser so it can get on with doing its painting.</p> <p>When Javascript in web browsers, you really need to take an event-driven approach to designing and coding your solutions. The classic example is prompting the user for information. In a non-event-driven world, you could do this:</p> <pre><code>// Non-functional non-event-driven pseudo-example askTheQuestion(); answer = readTheAnswer(); // Script pauses here doSomethingWithAnswer(answer); // This doesn't happen until we have an answer doSomethingElse(); </code></pre> <p>That doesn't work in an event-driven world. Instead, you do this:</p> <pre><code>askTheQuestion(); setCallbackForQuestionAnsweredEvent(doSomethingWithAnswer); // If we had code here, it would happen *immediately*, // it wouldn't wait for the answer </code></pre> <p>So for instance, <code>askTheQuestion</code> might overlay a div on the page with fields prompting the user for various pieces of information with an "OK" button for them to click when they're done. <code>setCallbackForQuestionAnswered</code> would really be hooking the <code>click</code> event on the "OK" button. <code>doSomethingWithAnswer</code> would collect the information from the fields, remove or hide the div, and do something with the info.</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