There are no two copies of the variable lying around. Javascript in web browsers is single-threaded (unless you are using new material for webmasters ). Thus, an anonymous function will never work, because Wait binds the interpreter.
You cannot use lively wait features in a Javascript browser; nothing else (and this is a bad idea in most other environments, even where they are possible). You should use callbacks instead. Here's a minimalist rework of this:
var interval_id; var countdowntimer = 0; function Wait(wait_interval, callback) { countdowntimer = wait_interval; interval_id = setInterval(function() { if (--countdowntimer <=0) { clearInterval(interval_id); interval_id = 0; callback(); } }, 1000); }
Edit Answering a question:
But at what point in processing this single thread does the so-called asynchronous call using setInterval really happen? Is it just between function calls? Surely not, what about functions that take a lot of time?
Quite a lot, yes - and therefore it is important that the functions are not lengthy. (Technically, this is not even between function calls, if you have a function that calls three other functions, the interpreter cannot do anything while this (external) function is running.) The interpreter essentially maintains a queue of functions that it needs to fulfill. It starts with the execution of any global code (rather, as a call to a large function). Then, when something happens (user input events, time for calling a callback scheduled through setTimeout been reached, etc.), the Interpreter pushes the calls that it must make to the queue. It always processes the call in front of the queue, and therefore things can stack up (for example, your setInterval calls, although setInterval is the special bit - it will not stop the subsequent callback if the previous one is still waiting in the queue). So think about when your code takes control and when it releases control (for example, upon return). The interpreter can only perform other actions after you release control and before he returns you again. And again, in some browsers (e.g. IE) the same stream is also used to draw the user interface, etc. Therefore, DOM inserts (for example) will not be displayed until you release the control back to the browser so that he could get while doing his painting.
When Javascript is in web browsers, you really need to take an event-oriented approach to developing and coding your solutions. A classic example tells the user information. In a non-event driven world, you can do this:
This does not work in an event driven world. Instead, you do the following:
askTheQuestion(); setCallbackForQuestionAnsweredEvent(doSomethingWithAnswer);
So, for example, askTheQuestion can impose a div on a page with fields offering the user various pieces of information with an OK button so that they can click when they are done. setCallbackForQuestionAnswered will indeed hook the click event to the OK button. doSomethingWithAnswer will collect information from the fields, delete or hide the div, and do something with the information.