Reputation: 1004
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?
Upvotes: 0
Views: 1097
Reputation: 26524
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);
Upvotes: 1
Reputation: 7466
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()
).
Upvotes: 2