I try to execute some functions with time out, this is code from class:
while($$('.visos_prekes ul li.simple').length > 0){
setTimeout(this.destroyByOne(), 5000);
}
destroyByOne: function(){
$$('.visos_prekes ul li.simple').each(function(e, key){
e.destroy();
if(key > 16){
return true;
}
});
},
but this function executes without time out. What I do wrong?
This line setTimeout(this.destroyByOne(), 5000);
should be:
setTimeout(this.destroyByOne, 5000);
Because you need to pass it the function handler aka variable.....and not the result of the function invocation ( this.destroyByOne()
).
This is the correct syntax of the setTimeout
function:
var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);
In which func
is the function (name of the function) you want to execute after delay milliseconds while code
in the alternate syntax, is a string of code you want to execute after delay milliseconds!
You can use setTimeout in either of the following:
setTimeout(this.destroyByOne, 5000);
setTimeout("this.destroyByOne()", 5000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.