I need to invoke some function given number of times through given delays. How should I do – declare variable for timer and pass it to invoking function for stopping timer in some moment or in loop (n times) invoke setTimeout once ( or some another approach to skeep delay time once) or other.Thanks.
edit to fix syntax eror
var timerID = null;
var n = 5;
this.timerID = setInterval(function(){
funcToInvoke(n,timerID){
if(invokeNumber == n){
clearInterval(timerID);
return;
}
else { do something}
}
},delay)
Your current method has a syntax problem, you can’t have a function parameter like
this.timerID). In fact, you should remove the wholefuncToInvokedeclaration, and declarenandtimerIDas local variables, so they will be available to the closure. Like this: