您想要 setTimeout
一次性事件和 setInterval
重复事件.
两者都有两个参数:一个函数和一个以毫秒为单位指定的时间间隔.
var delay_millis = 1500;
//will alert once, at least half a second after the call to setTimeout
var onceHandle = window.setTimeout(function() {
alert("Time has passed!");
}, delay_millis);
//will alert again and again
var repeatHandle = window.setInterval(function() {
alert("Am I annoying you yet?");
}, delay_millis);
Run Code Online (Sandbox Code Playgroud)
额外奖励:如果您通过调用这些函数来保留返回的值,则可以根据需要取消回调.
var shutUpShutUp = function() {
window.clearInterval(repeatHandle);
};
shutUpShutUp(); //now I can hear myself think.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4442 次 |
最近记录: |