Javascript “setInterval”与“setTimeout”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2696692/
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
'setInterval' vs 'setTimeout'
提问by Pranay Rana
What is the main difference between
之间的主要区别是什么
and
和
in JavaScript?
在 JavaScript 中?
回答by lunixbochs
setTimeout(expression, timeout);runs the code/function once after the timeout.
setTimeout(expression, timeout);超时后运行一次代码/函数。
setInterval(expression, timeout);runs the code/function in intervals, with the length of the timeout between them.
setInterval(expression, timeout);间隔运行代码/函数,它们之间的超时长度。
Example:
例子:
var intervalID = setInterval(alert, 1000); // Will alert every second.
// clearInterval(intervalID); // Will clear the timer.
setTimeout(alert, 1000); // Will alert once, after a second.
回答by deceze
setIntervalfires again and again in intervals, while setTimeoutonly fires once.
setInterval间隔一次又一次地setTimeout触发,而只触发一次。
See reference at MDN.
请参阅MDN 上的参考。
回答by MAS1
setTimeout():
设置超时():
It is a function that execute a JavaScript statement AFTERx interval.
它是一个执行 JavaScript 语句AFTERx 间隔的函数。
setTimeout(function () {
something();
}, 1000); // Execute something() 1 second later.
setInterval():
设置间隔():
It is a function that execute a JavaScript statement EVERYx interval.
它是一个执行 JavaScript 语句EVERYx 间隔的函数。
setInterval(function () {
somethingElse();
}, 2000); // Execute somethingElse() every 2 seconds.
The interval unit is in millisecondfor both functions.
间隔单位适用于millisecond这两个函数。
回答by Haris
setInterval()
设置间隔()
setInterval is a time interval based code execution method that has the native ability to repeatedly run specified script when the interval is reached. It should not be nested into its callback function by the script author to make it loop, since it loops by default. It will keep firing at the interval unless you call clearInterval().
setInterval 是一种基于时间间隔的代码执行方法,具有在达到间隔时重复运行指定脚本的本机能力。脚本作者不应将其嵌套到其回调函数中以使其循环,因为它默认循环。除非您调用 clearInterval(),否则它将继续按间隔触发。
if you want to loop code for animations or clocks Then use setInterval.
如果你想为动画或时钟循环代码然后使用 setInterval。
function doStuff() {
alert("run your code here when time interval is reached");
}
var myTimer = setInterval(doStuff, 5000);
setTimeout()
设置超时()
setTimeout is a time based code execution method that will execute script only one time when the interval is reached, and not repeat again unless you gear it to loop the script by nesting the setTimeout object inside of the function it calls to run. If geared to loop, it will keep firing at the interval unless you call clearTimeout().
setTimeout 是一种基于时间的代码执行方法,它只会在达到间隔时执行一次脚本,并且不会再次重复,除非您通过将 setTimeout 对象嵌套在它调用运行的函数中来使其循环脚本。如果适合循环,除非您调用 clearTimeout(),否则它将继续按间隔触发。
function doStuff() {
alert("run your code here when time interval is reached");
}
var myTimer = setTimeout(doStuff, 5000);
if you want something to happen one time after some seconds Then use setTimeout... because it only executes one time when the interval is reached.
如果你想在几秒钟后发生一次然后使用 setTimeout... 因为它只在达到间隔时执行一次。
回答by Daniel Earwicker
setIntervalrepeats the call, setTimeoutonly runs it once.
setInterval重复调用,setTimeout只运行一次。

