Opening New Pages With
I've tried what seems like dozens of different solutions to this all with different failures. Basically I've built an array that has a bunch of IDs in it and I and using javascrip
Solution 1:
You are evaluating go(i)
when it's passed as an argument. That function executes in the loop. Instead, you should return a function object, and that will be executed when the timer fires.
Do not do function(){go(i);}
, that's a classic closure error. You would end up calling the functiongo(i)
each time the event fired, but with the same, final, value of i
. The scope of i
is the enclosing function, so by the time the timeouts run it will have a value of Array.length - 1
.
Instead something like
window.setTimeout((function (j){returnfunction(j){go(j)}})(i), 60000*i;);
To spell that out;
(
function (j) {
var k = j;
returnfunction() {
go(k)
}
}
)(i);
This looks confusing, but that's the way JS works.
- It immediately executes a function with the value of
i
. - That returns a function object, but that function is in a closure in which the value of
i
bound at that point in time. - The function object is executed when the timeout event fires, with the value of
i
bound correctly.
Solution 2:
window.setTimeout(function(){ go(i); }, 60000*i;);
Here you go!
Post a Comment for "Opening New Pages With"