views:

1551

answers:

8

Hi,

I have a ajax javascript method that pulls data from a page etc.

I want this process to run on a timed interval, say every minute. But I don't want it to loop forever, so max out at 3 times.

What is the best way to implement this?

+1  A: 

You can use setInterval() and then inside the called function keep a count of how many times you've run the function and then clearInterval().

Or you can use setTimeout() and then inside the called function call setTimeout() again until you've done it 3 times.

Nosredna
+12  A: 

Like this:

var runCount = 0;    
function timerMethod() {
    runCount++;
    if(runCount > 3) clearInterval(timerId);

    //...
}

var timerId = setInterval(timerMethod, 60000);    //60,000 milliseconds
SLaks
+1 - this is the better solution. With `setInterval()` you avoid continously calling `setTimeout()` over and over, and the looped timing of `foo()` is slightly faster because you don't have to wait for `foo()` to process everything before setting up the next call.
zombat
Finally accepted! It's about time...
SLaks
+1  A: 
var testTimeInt = 3;
function testTime(){

    testTimeInt--;

    if(testTimeInt>0)
       setTimeOut("testTime()", 1000);

}


setTimeOut("testTime()", 1000);
andres descalzo
+2  A: 

Use setInterval, be sure to get a reference.

var X=setInterval(....);

Also, have a global counter

var c=0;

Inside the function called by the setIntervale do:

c++;
if(c>3) window.clearInterval(X);
Itay Moav
+3  A: 

A reusable approach

function setMaxExeuctionInterval( callback, delay, maxExecutions )
{
  var intervalCallback = function()
  {
    var self = intervalCallback;
    if ( 'undefined' == typeof self.executedIntervals )
    {
      self.executedIntervals = 1;
    }
    if ( self.executedIntervals == maxExecutions )
    {
      clearInterval( self.interval )
    }
    self.executedIntervals += 1;
    callback();
  };
  intervalCallback.interval = setInterval( intervalCallback, delay );
}

// console.log requires Firebug
setMaxExeuctionInterval( function(){ console.log( 'hi' );}, 700, 3 );
setMaxExeuctionInterval( function(){ console.log( 'bye' );}, 200, 8 );
Peter Bailey
+10  A: 

A closure-based solution, using setInterval() and clearInterval():

// define a generic repeater
var repeater = function(func, times, interval) {
  var ID = window.setInterval( function(times) {
    return function() {
      if (--times <= 0) window.clearInterval(ID);
      func();
    }
  }(times), interval);
};

// call the repeater with a function as the argument
repeater(function() {
  alert("stuff happens!");
}, 3, 60000);

EDIT: Another way of expressing the same, using setTimeout() instead:

var repeater = function(func, times, interval) {
  window.setTimeout( function(times) {
    return function() {
      if (--times > 0) window.setTimeout(arguments.callee, interval);
      func();
    }
  }(times), interval);
};

repeater(function() {
  alert("stuff happens!");
}, 3, 2000);

Maybe the latter is a bit easier to understand.

In the setTimeout() version you can ensure that the next iteration happens only after the previous one has finished running. You'd simply move the func() line above the setTimeout() line.

Tomalak
+1 I like your closure-based solution better than my object property based one below. At least we were both thinking in terms of re -usability :D
Peter Bailey
+3  A: 

This anonymous function (it doesn't introduce any new globals) will do what you need. All you have to do is replace yourFunction with your function.

(function(fn, interval, maxIterations) {
    var iterations = 0,
    id = setInterval(function() {
     if (++iterations > maxIterations)
      return clearInterval(id);

     fn();
    }, interval);
})(yourFunction, 60000, 3);
Eli Grey
+1  A: 

To extend Tomalak function:

If you want to know how many cycles are left:

var repeater = function(func, times, interval) {
    window.setTimeout( function(times) {
    return function() {
      if (--times > 0) window.setTimeout(arguments.callee, interval);
      func(times);
    }
  }(times), interval);
}

and use:

repeater(function(left){
    //... (do you stuff here) ...
    if(left == 0) {
        alert("I'm done");
    }
}, 3, 60000);
lepe