Kri*_*hat -3 javascript setinterval
我已经尝试了设置间隔,因此应该在2秒后调用函数computercardArrange()只调用一次.但它在2秒后连续调用函数computercardArrange().如何停止,以便在2秒后只调用一次computercardArrange()函数.下面是代码.
function timer() {
setInterval(function(){
computercardArrange(); // This function should be called only one time after 2 second.
}, 2000);
}
Run Code Online (Sandbox Code Playgroud)
只要使用的setTimeout代替
function timer() {
setTimeout(function(){
computercardArrange(); // This function should be called only one time after 2 second.
}, 2000);
}
Run Code Online (Sandbox Code Playgroud)
如果我想调用该函数10次并停止间隔怎么办?
var callCount = 0 ;
function timer() {
var intervalIdentifier = setInterval(function(){
computercardArrange();
if (++ callCount == 10) {
clearInterval(intervalIdentifier);
}
}, 2000);
}
Run Code Online (Sandbox Code Playgroud)