Javascript 设置/清除间隔不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15795730/
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
Javascript set/clearinterval is not working
提问by Robin Timman
How can i stop a interval in javascript?
如何在javascript中停止间隔?
why does the interval not stop?
为什么间隔不停止?
setInterval(alarm, 500);
window.clearInterval(alarm);
also tried:
也试过:
window.setInterval(alarm, 500);
window.clearInterval(alarm);
always the same problem :(
总是同样的问题:(
still doesn't work:
仍然不起作用:
var switc = 1;
getValue();
function getValue (){
if(switc == 1){
var myTimer = window.setInterval(alarm, 500);
}
else if(switc == 0){
window.clearInterval(myTimer);
}
}
function alarm(){
console.log("test");
}
回答by epascarello
When you call setInterval it returns a integer that you use to cancel the event. Store that into a variable and use that variable to cancel the event.
当您调用 setInterval 时,它会返回一个用于取消事件的整数。将其存储到变量中并使用该变量取消事件。
var myTimer = window.setInterval(alarm, 500);
window.clearInterval(myTimer);
EDIT:
编辑:
Your code does not work since myTimer is a local variable and is reset every single time you call the function!
您的代码不起作用,因为 myTimer 是一个局部变量,每次调用该函数时都会重置!
Make it global.
让它全球化。
var myTimer = null;
function getValue (){
if(switc == 1){
myTimer = window.setInterval(alarm, 500);
}
...
MDN Docs: window.setInterval
MDN 文档:window.setInterval
Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.
重复调用函数或执行代码片段,每次调用该函数之间有固定的时间延迟。
Syntax
句法
var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);
where
在哪里
intervalID
is a unique interval ID you can pass to clearInterval().func
is the function you want to be called repeatedly.code
in the alternate syntax, is a string of code you want to be executed repeatedly (using this syntax is not recommended for the same reasons as using eval())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. Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer. If you want to enable this functionality on that browser you must use a compatibility code (see the Callback arguments paragraph).
intervalID
是可以传递给 clearInterval() 的唯一间隔 ID。func
是你想要重复调用的函数。code
在替代语法中,是您想要重复执行的代码字符串(不建议使用此语法,原因与使用 eval() 的原因相同)delay
是 setInterval() 函数在每次调用 func 之前应该等待的毫秒数(千分之一秒)。与 setTimeout 一样,强制执行最小延迟。请注意,将附加参数传递给第一种语法中的函数在 Internet Explorer 中不起作用。如果要在该浏览器上启用此功能,则必须使用兼容性代码(请参阅回调参数段落)。
回答by Pointy
That code evinces a misunderstanding of the API. The setInterval()
function takes two arguments: a function to call, and a number representing the number of milliseconds between calls. The function returnsa token (a number) that identifies that particular interval timer. The token can be passed to clearInterval()
to cancel the timer.
该代码表明对 API 的误解。该setInterval()
函数有两个参数:一个要调用的函数和一个表示调用之间的毫秒数的数字。该函数返回一个标识该特定间隔计时器的标记(一个数字)。可以传递令牌clearInterval()
以取消计时器。
回答by hop
You are tring to clear an non existent interval. Assign the id returned by setInterval() to an variable and use it in clearInterval().
您正在尝试清除不存在的间隔。将 setInterval() 返回的 id 分配给一个变量并在 clearInterval() 中使用它。
In your case alarm is the function that executes and its not the intervals id
在您的情况下,警报是执行的功能,而不是间隔 id
var interval = setInterval(alarm, 500);
clearInterval(interval);
回答by DaveB
var timer = setInterval(alarm, 500);
Window.clearInterval(timer);
function alarm() {
// Do stuff
}
You need to save the handle of the interval to a variable so you can reference it later when you want to clear/stop it.
您需要将时间间隔的句柄保存到一个变量中,以便稍后在您想要清除/停止它时引用它。