It 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.
For example:
The above example will create an alert box with the message "Hello, World!" after 5 seconds.
setInterval is very similar to the above, the key difference is that it repeatedly runs the same Javascript at the specified time.
For example:
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! Published on May 5, 2009 Tags: setTimeout()
| setInterval()
| JavaScript
Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article
that you just finished reading.
No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner
or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in
and improve your skillset with any of the tutorials below.
setTimeout allows us to tell the user's browser to perform some Javascript in X milliseconds. The Javascript executed can be a function call, a single line of Javascript, etc...
setTimeout("alert('Hello, World!')", 5000);
setInterval("alert('Hello, World!')", 5000);
Related Posts
Tutorials
Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.