2012-05-13 17 views
3

我想在运行代码时更改setInterval函数的时间。setInterval(function(),time)更改运行时的时间

我试试这个

<script type="text/javascript"> 
     $(function() { 
      var timer; 
      function come() { alert("here"); } 
      timer = setInterval(come, 0); 
      clearInterval(timer); 
      timer = setInterval(come, 10000); 
     }); 
    </script> 

首先setInterval的不行!

+0

你是什么意思“不工作?” –

+0

http://stackoverflow.com/a/29823252/1478566 – vbarbarosh

回答

11

你清除下一行的间隔,所以第一个不会工作,因为它得到正确的清理掉:

 timer = setInterval(come, 0); 
     clearInterval(timer); 
     timer = setInterval(come, 10000); 

而且,gdoron说,设置什么ISN的间隔不是真的有效,也不是一个好主意,而是使用setTimeout,或者如果不需要延迟就直接运行函数。

 come(); 
     clearInterval(timer); 
     timer = setInterval(come, 10000); 
+0

使用上面的代码时,come()函数停止一秒钟,然后再次运行...有什么办法可以避免come函数的停止?... – Naveen

1
timer = setInterval(come, 0); // zero isn't a valid interval... 

你可能想:在MDN

come(); 
timer = setInterval(come, 10000); 

文档:

延迟为毫秒(千分之一秒)的数量的setInterval()函数每前应等待调用func。和setTimeout一样,强制执行最小延迟。

和:

历史浏览器实现的setTimeout()“夹紧”:连续的setTimeout()的延迟比“最小延迟”限制较小被迫使用至少最低要求延迟。最小延迟DOM_MIN_TIMEOUT_VALUE为4毫秒(存储在Firefox的首选项中:dom.min_timeout_value),DOM_CLAMP_TIMEOUT_NESTING_LEVEL为5毫秒。

+0

当然是,为什么不呢? –

+0

@GregHewgill。文档... – gdoron

+0

对不起,我误读了,并想'setTimeout()'。 –

3

你不能。您将需要使用的setTimeout,并重复调用它:

var timer; // current timeout id to clear 
function come(){ /* do something */}; 
var time; // dynamic interval 

(function repeat() { 
    come(); 
    timer = setTimeout(repeat, time); 
})(); 

有了这个,你可以设置不同的“间隔”每次执行功能repeat时间得到应用。然而,如果在超时期间改变time,则没有任何变化,您需要停止超时。

+1

'timer'进入游戏的位置在哪里? – gdoron

+0

谢谢,我错过了显而易见的... – Bergi

+0

谢谢,这是我需要的。也许你可以添加,你可以用'clearTimeout(timer)'在'come()内部中断循环;' – Sonic750

2

无法直接更改函数触发的时间间隔。你可以做的最好的是取消一个时间间隔,并设置一个具有相同功能和更新计时器的新时间间隔。下面是这样做的一个可能的方式:

timer = { 
    timers:{}, 
    inc:0, 
    start:function(cb,gap) { 
     var key = inc; 
     inc++; 
     timer.timers[key] = [setInterval(cb,gap),cb]; 
     return key; 
    }, 
    stop:function(id) { 
     if(!timer.timers[id]) return; 
     clearInterval(timer.timers[id][0]); 
     delete timer.timers[id]; 
    }, 
    change:function(id,newgap) { 
     if(!timer.timers[id]) return; 
     clearInterval(timer.timers[id][0]); 
     setInterval(timer.timers[id][1],newgap); 
    } 
}; 

用法:

var myTimer = timer.start(function() {....},1000); 
// calls every second 

timer.change(myTimer,5000); 
// now calls every five seconds 
0

我知道这个帖子是老了,但我需要类似的东西,也许有人需要它。

这是一个没有setInterval的版本,基于来自其他反应的代码(Niet the Dark Absol)。

function timer() 
{ 
    var timer = { 
     running: false, 
     iv: 5000, 
     timeout: false, 
     cb : function(){}, 
     start : function(cb,iv,sd){ 
      var elm = this; 
      clearInterval(this.timeout); 
      this.running = true; 
      if(cb) this.cb = cb; 
      if(iv) this.iv = iv; 
      if(sd) elm.execute(elm); 
      this.timeout = setTimeout(function(){elm.execute(elm)}, this.iv); 
     }, 
     execute : function(e){ 
      if(!e.running) return false; 
      e.cb(); 
      e.start(); 
     }, 
     stop : function(){ 
      this.running = false; 
     }, 
     set_interval : function(iv){ 
      clearInterval(this.timeout); 
      this.start(false, iv); 
     } 
    }; 
    return timer; 
} 

用法:

var timer_1 = new timer(); 
timer_1.start(function(){ 
    //magic here 
}, 2000, false); 

var timer_2 = new timer(); 
timer_2.start(function(){ 
    //more magic here 
}, 3000, true); 

//change the interval 
timer_2.set_interval(4000); 

//stop the timer 
timer_1.stop(); 

启动功能的最后一个参数是一个布尔值,如果函数需要在0

来运行你也可以在这里找到脚本:https://github.com/Atticweb/smart-interval