Javascript 如何使用 setInterval 和 clearInterval?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5978519/
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-23 19:44:05  来源:igfitidea点击:

How to use setInterval and clearInterval?

javascriptjquery

提问by Raj

function doKeyDown(event) {
    switch (event.keyCode) {
    case 32:
        /* Space bar was pressed */
        if (x == 4) {
            setInterval(drawAll, 20);
        }
        else {
            setInterval(drawAll, 20);
            x += dx;
        }
        break;
    }
}

Hi all,

大家好,

I want to call drawAll()oncenot creating a loopthat call drawAllagain and again, should I use recursive method for that or should I use clearInterval?

我想调用drawAll()一次而不是创建一个一次又一次调用的循环drawAll我应该为此使用递归方法还是应该使用clearInterval

Also please tell me to use clearInterval? Thanks :)

还请告诉我使用clearInterval?谢谢 :)

回答by T.J. Crowder

setIntervalsets up a recurringtimer. It returns a handle that you can pass into clearIntervalto stop it from firing:

setInterval设置一个循环计时器。它返回一个句柄,您可以传入该句柄clearInterval以阻止其触发:

var handle = setInterval(drawAll, 20);

// When you want to cancel it:
clearInterval(handle);
handle = 0; // I just do this so I know I've cleared the interval

On browsers, the handle is guaranteed to be a number that isn't equal to 0; therefore, 0makes a handy flag value for "no timer set". (Other platforms may return other values; NodeJS's timer functions return an object, for instance.)

在浏览器上,句柄保证是一个不等于0;的数字。因此,0为“未设置计时器”设置一个方便的标志值。(其他平台可能会返回其他值;例如,NodeJS 的计时器函数会返回一个对象。)

To schedule a function to onlyfire once, use setTimeoutinstead. It won't keep firing. (It also returns a handle you can use to cancel it via clearTimeoutbefore it fires that one time if appropriate.)

要将函数安排为触发一次,请setTimeout改用。它不会继续射击。(clearTimeout如果合适,它还返回一个句柄,您可以在它触发一次之前用来取消它。)

setTimeout(drawAll, 20);

回答by Joshua - Pendo

clearIntervalis one option:

clearInterval是一种选择:

var interval = setInterval(doStuff, 2000); // 2000 ms = start after 2sec 
function doStuff() {
  alert('this is a 2 second warning');
  clearInterval(interval);
}

回答by dlev

Use setTimeout(drawAll, 20)instead. That only executes the function once.

使用setTimeout(drawAll, 20)来代替。那只执行一次函数。

回答by PJ3

I used angular with electron,

我用角度和电子,

In my case, setIntervalreturns a Nodejs Timer object. which when I called clearInterval(timerobject)it did not work.

就我而言,setInterval返回一个 Nodejs Timer 对象。当我调用clearInterval(timerobject)它时它不起作用。

I had to get the id first and call to clearInterval

我必须先获取 id 并调用 clearInterval

clearInterval(timerobject._id)

clearInterval(timerobject._id)

I have struggled many hours with this. hope this helps.

我为此苦苦挣扎了好几个小时。希望这可以帮助。

回答by HynekS

Side note – if you want to use separate functions to set & clear interval, the interval variable have to be accessible for all of them, in 'relative global', or 'one level up' scope:

旁注 - 如果您想使用单独的函数来设置和清除间隔,则必须在“相对全局”或“上一级”范围内对所有函数都可以访问间隔变量:

var interval = null;    

function startStuff(func, time) {
    interval = setInterval(func, time);
}

function stopStuff() {
    clearInterval(interval);
}