Want a javascript function to run every minute, but max 3 times

后端 未结 9 999
终归单人心
终归单人心 2020-12-28 17:43

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 forev

9条回答
  •  -上瘾入骨i
    2020-12-28 18:27

    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);
    

提交回复
热议问题