lun*_*chs 400
setTimeout(expression, timeout);
超时后运行代码/函数一次.
setInterval(expression, timeout);
以间隔运行代码/函数,以及它们之间的超时长度.
例:
var intervalID = setInterval(alert, 1000); // Will alert every second.
// clearInterval(intervalID); // Will clear the timer.
setTimeout(alert, 1000); // Will alert once, after a second.
Run Code Online (Sandbox Code Playgroud)
MAS*_*AS1 62
的setTimeout():
它是一个执行JavaScript语句AFTER
x interval 的函数.
setTimeout(function () {
something();
}, 1000); // Execute something() 1 second later.
Run Code Online (Sandbox Code Playgroud)
的setInterval():
它是一个执行JavaScript语句EVERY
x interval 的函数.
setInterval(function () {
somethingElse();
}, 2000); // Execute somethingElse() every 2 seconds.
Run Code Online (Sandbox Code Playgroud)
间隔单元millisecond
用于两种功能.
Har*_*ris 26
setInterval是一种基于时间间隔的代码执行方法,具有在达到间隔时重复运行指定脚本的本机能力.它不应该被脚本作者嵌入到它的回调函数中以使其循环,因为它默认循环.除非你调用clearInterval(),它将继续按间隔触发.
如果你想为动画或时钟循环代码然后使用setInterval.
function doStuff() {
alert("run your code here when time interval is reached");
}
var myTimer = setInterval(doStuff, 5000);
Run Code Online (Sandbox Code Playgroud)
setTimeout是一种基于时间的代码执行方法,它只会在达到间隔时执行一次脚本,并且不会再次重复,除非您通过将setTimeout对象嵌入其调用的函数内部来循环脚本.如果适合循环,除非你调用clearTimeout(),它将继续按间隔触发.
function doStuff() {
alert("run your code here when time interval is reached");
}
var myTimer = setTimeout(doStuff, 5000);
Run Code Online (Sandbox Code Playgroud)
如果你想在几秒钟之后发生一次事情然后使用setTimeout ...因为它只在达到间隔时执行一次.
归档时间: |
|
查看次数: |
230051 次 |
最近记录: |