2009-08-07 40 views
16

我有JavaScript方法,从一个页面等想要一个javascript函数为每分钟运行,但最多3次

我想这个过程就定时间隔运行拉数据的AJAX,说每一分钟。 但我不希望它永远循环,所以最多3次。

实现此目的的最佳方法是什么?

+0

快速和肮脏,但为什么不只是每次请求运行时有一个计数器增量,如果(计数器<3){intervalRun();}? – Optimate 2009-08-07 16:05:22

+0

@optimate:什么是intervalRun()? – Nosredna 2009-08-07 16:07:01

+0

他打算说'setInterval'。 – SLaks 2009-08-07 16:29:21

回答

40

像这样:

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

    //... 
} 

var timerId = setInterval(timerMethod, 60000); //60,000 milliseconds 
+3

+1 - 这是更好的解决方案。使用'setInterval()'可以避免反复连续调用'setTimeout()',并且'foo()'的循环时序稍微快一点,因为您不必等待'foo()'处理所有内容设置下一个电话。 – zombat 2009-08-07 16:29:38

+0

@zombat实际上,在setInterval()的很多场景中,setTimeout()往往是首选,因为setInterval()会在第一个函数*开始运行*之后60秒后调用该函数,还未完成。特别是现在Chrome和Firefox正在限制背景选项卡,如果您不想一次堆叠这三个函数调用,那么setInterval()会更加可靠。 – 2017-04-17 22:53:52

1

你可以使用setInterval(),然后在被调用的函数中保存你已经运行了多少次函数然后clearInterval()。

或者你可以使用setTimeout(),然后在被调用的函数中再次调用setTimeout(),直到你做了3次。

0
var testTimeInt = 3; 
function testTime(){ 

    testTimeInt--; 

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

} 


setTimeOut("testTime()", 1000); 
2

使用setInterval的,一定要得到一个参考。

var X=setInterval(....); 

而且,有一个全球性的反

var c=0; 

由setIntervale调用的函数里面做:

c++; 
if(c>3) window.clearInterval(X); 
4

可重复使用的方法

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

基于闭合解决方案,使用setInterval()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); 

编辑:表达相同,使用setTimeout()而不是另一种方式:

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

也许后者是有点更容易理解。

setTimeout()版本中,您可以确保在之前的下一次迭代只发生在上一次运行完成。您只需将func()以上setTimeout()行。

+0

+1我喜欢你的基于闭包的解决方案,比我的对象属性更好。至少我们都在考虑可重用性:D – 2009-08-07 16:47:11

3

这个匿名函数(它不会引入任何新的全局变量)会做你所需要的。你所要做的就是用你的功能替换yourFunction

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

     fn(); 
    }, interval); 
})(yourFunction, 60000, 3); 
3

延长Tomalak功能:

如果你想知道有多少次是左:

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

及用途:

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

,你可以在使用setInterval

var count = 0; 

var interval = setInterval(yourFunction(), 1000); 

function yourFunction(){ 

    clearInterval(interval); 
    if(count < 3){ 

    count ++; 
    interval = setInterval(yourFunction(), 1000); 
    } 

// your code 


} 
+0

请编辑您的答案并格式化代码以使其可读。 – kleopatra 2012-11-27 09:52:05

相关问题