1

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)
3
  • 1
    Yes, setInterval (or a "recursive" call to setTimeout) is better than a loop that invokes setTimeout several times. Commented Nov 28, 2012 at 13:14
  • does approach I show is common used (and right) ? Commented Nov 28, 2012 at 13:16
  • Your funcToInvoke is a syntax error (or parsed as an invocation + a code block, which is not what you want). Please fix it, and notice that the this keyword in the interval-function does not point to your object. Commented Nov 28, 2012 at 13:18

3 Answers 3

3

Yes, the approach is common and better than calling setTimeout in a loop (with a fixed number of times). It is more performant than that and also more flexible, because the interval will be stopped dynamically (might check for a future condition).

However, your code is a bit messy. Fixed:

// Assuming we a have
//  n
//  delay
//  funcToInvoke
// and execute in context of some object
var that = this,
    numberOfInvokes = 0;
this.timer = setInterval(function() {
     // "this" points to the global object
     if (numberOfInvokes == n)
          clearInterval(that.timer);
     else
          funcToInvoke(numberOfInvokes);
     numberOfInvokes++;
}, delay);
Sign up to request clarification or add additional context in comments.

1 Comment

This is what my answer should have looked like if I'd been more careful then. +1.
2

Your current method has a syntax problem, you can't have a function parameter like this.timerID). In fact, you should remove the whole funcToInvoke declaration, and declare n and timerID as local variables, so they will be available to the closure. Like this:

// Don't forget to define n here!
var n = 5;
// Change timerID to local var instead of property
var timerID = null;
timerID = setInterval(function(){ 
    if(invokeNumber == n){
        clearInterval(timerID);
        return;
    } else { 
        //do something
    }
    // You can setTimeout again anywhere in this function if needed
}, delay);

2 Comments

This still suffers the same problem. Either it is supposed to be a useless function declaration (which it is not), or a call to funcToInvoke plus a statement block.
@Bergi Yes, that was an oversight funcToInvoke is unnecessary. Edited.
1

If you want an approximate delay, setInterval is probably ok. If you want a more precise interval, then repeated calls to setTimeout are better as you can adjust the length of time to the next call based on the time since the last call.

E.g. for a clock ticking every second, you can do repeated calls to setTimeout, setting the lag to just after the next full second.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.