I have read the relevant pages on w3schools and other similar questions here but cannot seem to understand what's wrong about the following bit :
var myfunc03 = function (i) {
document.getElementById('d01').innerHTML += 100-i+"<br>";
};
var myFunc01 = function() {
i=0;
while (i<100) {
setTimeout(myfunc03(i), 1000)
i++;
}
};
when myFunc01();
is run.
There's no pause whatsoever and all possible values for i is listed at once.
Is there a logical mistake here?
i
) from within a closure. When the timeout fires,i
will have changed to whatever the last one is. – WhiteeyesetTimeout
expects aFunction
as the first parameter but you're passing the result ofmyfunc03
(which isunderfined
because you're invoking it) – Bunni