Javascript – how to make something happen after some time

No Comments »

I’m writing this post because I’ve been asked a million times about this and it’s easier to point someone to a link than to explain it again :-) .

In Javascript, executing something after a specified time couldn’t be easier – you’ve got the setTimeout method. Just be careful to call it the right way. It takes two arguments:

-> the method to be called after the specified time passed. I repeat, a method, not a method call.
-> the amount of time, specified in milliseconds.

For example, using script.aculo.us, I can make the “feedback” div fade away in 10 seconds after rendering like this:

window.setTimeout(function() {Effect.Fade(‘feedback’);}, 10000);

(and NOT window.setTimeout(Effect.Fade(‘feedback’), 10000}; – also keep in mind Javascript is case sensitive)

Post a Comment