Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait before firing a function to execute in JavaScript

How can I set a delayed trigger in JavaScript to execute a function after a specified amount of time?

My program will wait for 5 seconds to execute demo(); and if it fails to start demo within 5 seconds I need to execute sample() automatically.

Is this possible to do in JavaScript?

like image 926
Aadi Avatar asked Apr 16 '10 06:04

Aadi


1 Answers

You can invoke functions after a period of time with setTimeout

setTimeout(demo, 5000);

I'm not sure that I get the "if it is fail to start demo with in 5 seconds" part of your question, because the above code will execute demo() in 5 seconds.

like image 79
GlenCrawford Avatar answered Oct 19 '22 23:10

GlenCrawford