Published on May 5, 2009 by Jamie MunroIt feels like it has been a lifetime since my last blog! I must apologize for the long delay, but between finding the time and finding a good topic to blog about, it's being difficult.
In today's article, it's been a while since I've needed to use either the setTimeout or setInterval functions and it seems many people are not familiar with the setInterval function.
Let's start by describing the two.
setTimeout("alert('Hello, World!')", 5000);
The above example will create an alert box with the message "Hello, World!" after 5 seconds.
setInterval("alert('Hello, World!')", 5000);
The above example will create an alert box with the message "Hello, World!" EVERY 5 seconds.
So, when should you use setTimeout and when should you use setInterval? Well, the answer is quite simple, if you just want to execute the code once, use setTimeout. If you want to execute the code repeatively, use setInterval.
No more setTimeout's that call a function and inside of that function another setTimeout call!