I'd like to wait 1 minute and then execute a function f(). I have found out that in Javascript there isn't a sleep() function but I can use setInterval() or setTimeout() functions.
The window.setInterval() function works, but this is not what I want. I want to execute f() only once. I tried to use the setTimeout() function as follows.
var MyNamespace {
...
f: function() {
},
...
click: function() {
...
setTimeout("this.f()", 60000); // f() is never executed
// setTimeout(this.f(), 60000); f() is executed immediately without timeout
// window.setTimeout(...) doesn't help
},
...
}
What could be wrong here?
The code is part of a Firefox extension.
There's your problem: "this.f()"
. Replace it with:
setTimeout(this.f.bind(this), 60000);
and it should work. Passing a string to setInterval()
is never a good solution, just like using eval()
.