I'm using jQuery to setup a timer or interval loop on a few elements to check them every couple seconds. I've tried setting a timer and checking if I should restart it, or setting and interval and checking if I should stop it.
Although simplified, this is basically what I need:
var mytimers = new Array();
$('div.items').each(function() {
myID = $(this).attr('id');
mytimers[myID] = setInterval( function() { myFunction(myID) } , 3000)
});
function myFunction(param) {
alert(param);
if (something()) {
clearInterval(mytimers[param]);
}
}
The ID's for class items are id_1, id_2, id_3. But I simply get 3 alerts all giving id_3. In my code I started off trying to pass 'this', but kept simplifying it to figure out the issue.
How can I get it to copy the variable to a new address each time? I know I need to use closures. It seems to be referencing the other var no mater what.
I tried simplifying it to a loop with timers like so:
function tester(item) {
return function() {
alert(item);
};
}
for(x=1;x<=3;x++) {
setTimeout( '(function() { tester(x) })(x)' , 3000);
}
But I think I'm just making my problem worse and that doesn't seem to do anything.
I've searched for previous questions, but most are filled with tons of extra code rather than cutting it down the the specific issue and are solved in some other manner. I'd like to understand how this works better by getting this example working. I did manage, while writing this, to figure out I can set off the timer with a helping function.
function tester(item)
alert(item);
function myTimer(item)
setInterval( function() { tester(item); }, 3000);
for(x=1;x<=3;x++)
myTimer(item);
How can this be done without that? Is there some better way?