Javascript setInterval(function(),time) 在运行时更改时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10576106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:56:36  来源:igfitidea点击:

setInterval(function(),time) change time on runtime

javascriptjquery

提问by altandogan

I want to change setInterval function time when my code is running.

我想在我的代码运行时更改 setInterval 函数时间。

I try this

我试试这个

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

First SetInterval does not work!

首先 SetInterval 不起作用!

回答by adeneo

You're clearing the interval on the next line, so the first one wont work, as it gets cleared right away :

您正在清除下一行的间隔,因此第一个将不起作用,因为它会立即被清除:

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

Also, as gdoron says, setting a interval of nothing isn't really valid, and not a really good idea either, use setTimeout instead, or just run the function outright if no delay is needed.

此外,正如 gdoron 所说,设置一个空间隔并不是真正有效的,也不是一个很好的主意,而是使用 setTimeout ,或者如果不需要延迟就直接运行该函数。

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

回答by Bergi

You can't. You will need to use setTimeout, and call it repetitively:

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

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

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

With this you can set a different "interval" to be applied each time the function repeatis executed. Yet, nothing changes if alter timeduring a timeout, you'd need to stop the timeout for that.

有了这个,您可以设置每次repeat执行函数时要应用的不同“间隔” 。但是,如果time在超时期间更改没有任何变化,您需要为此停止超时。

回答by Niet the Dark Absol

There is no way to directly change the interval at which a function fires. The best you can do is cancel an interval and set a new one with the same function and updated timer. Here's a possible way of doing it:

无法直接更改函数触发的间隔。您能做的最好的事情是取消一个间隔并设置一个具有相同功能和更新计时器的新间隔。这是一种可能的方法:

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

Usage:

用法:

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

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

回答by gdoron is supporting Monica

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

You probably wants:

你可能想要:

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

docs on MDN:

MDN 上的文档:

delay is the number of milliseconds (thousandths of a second) that the setInterval() function should wait before each call to func. As with setTimeout, there is a minimum delay enforced.

delay 是 setInterval() 函数在每次调用 func 之前应该等待的毫秒数(千分之一秒)。与 setTimeout 一样,强制执行最小延迟。

And:

和:

Historically browsers implement setTimeout() "clamping": successive setTimeout() calls with delay smaller than the "minimum delay" limit are forced to the use at least the minimum delay. The minimum delay, DOM_MIN_TIMEOUT_VALUE, is 4 ms (stored in a preference in Firefox: dom.min_timeout_value), with a DOM_CLAMP_TIMEOUT_NESTING_LEVEL of 5ms.

历史上浏览器实现 setTimeout() “钳制”:延迟小于“最小延迟”限制的连续 setTimeout() 调用被强制使用至少最小延迟。最小延迟 DOM_MIN_TIMEOUT_VALUE 为 4 毫秒(存储在 Firefox 中的首选项中:dom.min_timeout_value),DOM_CLAMP_TIMEOUT_NESTING_LEVEL 为 5 毫秒。

回答by Jaapze

I know this post is old, but i needed something similar, maybe someone needs it.

我知道这篇文章很旧,但我需要类似的东西,也许有人需要它。

This is a version without setInterval, based on the code from the other reaction (Niet the Dark Absol).

这是一个没有 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;
}

Usage:

用法:

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

The last parameter of the start function is a boolean if the function needs to be run at 0.

如果函数需要在 0 处运行,则 start 函数的最后一个参数是一个布尔值。

You can also find the script here: https://github.com/Atticweb/smart-interval

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