Saturday, December 2, 2023
HomeVideo EditingTiming Occasions in JavaScript: setTimeout and setInterval

Timing Occasions in JavaScript: setTimeout and setInterval

JavaScript timing occasions are used to run code at a selected time or after a selected time period. These occasions are steadily utilized in net growth for animations, slide shows, and different dynamic options that decision for updates or modifications over time.

The window object, which gives the setTimeout and setInterval strategies for scheduling occasions, is in command of controlling JavaScript timing occasions. In JavaScript, the unit of time measurement for timing occasions is milliseconds, which gives a very good steadiness between precision and practicality. 1000 milliseconds represents one second, 2000 milliseconds represents two seconds, and so forth.

Delayed Occasions With the setTimeout Perform

The setTimeout operate is used to execute a bit of code after a predetermined period of time. This operate’s syntax is as follows:

setTimeout(operate, milliseconds);

This executes a operate, after ready for a specified variety of milliseconds.

The primary parameter is a operate to be executed. The second parameter specifies what number of milliseconds to attend earlier than execution. If for instance, you need a message saying “Hey there” to pop up on the display after three seconds, that is how the code would look: 

<button onclick="setTimeout(timingFunction, 3000)">Click on Me!</button>

operate timingFunction() {
    alert('Hey there');

Repeated Occasions With the setInterval Perform

The setInterval operate is used to execute a bit of code repeatedly at predetermined intervals. This operate’s syntax is as follows:

setInterval(operate, milliseconds);

The primary parameter is a operate to be executed. The second parameter specifies the interval between every execution. Let’s construct a easy digital clock that shows the present time to show this. The clock executes the operate digitalClock each one second, similar to how a digital watch works.

Clearing Timing Occasions

We’d in some unspecified time in the future wish to cease a operate from operating after it has ran for just a few occasions or a specified time period. To cease timed execution, prefix clear to the timing occasion operate you might be working with.

The clearTimeout() operate is used to cease the execution of a operate specified within the setTimeout() operate. The setTimeout() operate returns a timing id. You should utilize this id to clear the timeout later with clearTimeout().

<button onclick="timingID = setTimeout(timingFunction, 3000)">Begin</button>
<button onclick="clearTimeout(timingID)">Finish</button>

operate timingFunction() {
  alert("Hey there");

When you click on the Begin button, it waits for 3 seconds earlier than the “Hey there” message is alerted. When you occur to click on the Finish button earlier than that 3 seconds is up, it stops the operate from being executed.

Equally, the clearInterval() operate is used to cease the execution of a operate specified within the setInterval() operate. Identical to for setTimeout(), it makes use of the timer id returned from the setInterval() operate as an argument.  To show this, we’ll add a cease button to the digital clock instance above.

The clocks begins working as soon as the web page is loaded however if you click on the Cease time button, it stops. 

Significance of Clearing the Timing Occasion Features

When utilizing timing occasions in JavaScript, it is essential to remember the fact that the occasions should be cleared utilizing the clearTimeout() or clearInterval() operate for the next causes:

To Stop Reminiscence Leaks

A reference to the operate you wish to run is saved in reminiscence if you set a timeout or interval in JavaScript. When a timeout or interval is not required, it’s best to take away the reference from reminiscence to keep away from reminiscence use from rising. Efficiency issues could finally outcome from this, particularly in case you’re coping with lots of information.

To Stop Sudden Code Habits

You probably have an interval that refreshes the UI each second however does not clear it when the consumer navigates away from the web page, the interval will preserve firing even when the consumer is not viewing the web page. 

By calling clearTimeout() or clearInterval(), you’ll be able to cancel the excellent timer and stop these points from occurring. This helps to keep away from reminiscence leaks and surprising conduct whereas making certain that your code is efficient and predictable.

Instance Challenge Combining Each Timing Occasions

This is a bit undertaking to demonstrates the 2 foremost JavaScript timing occasions in a extra superior performance. It is a easy net web page that makes use of setInterval() to show a random quantity each second and setTimeout() to cease the show after 20 seconds.

Listed here are just a few concepts for different little studying initiatives you may attempt to sharpen your expertise with the interval and timeout features:

  • countdown timer
  • random quote generator
  • slideshow animation
  • JavaScript quiz recreation with timer


Timing occasions in JavaScript are a useful gizmo for net builders, because it allows them to create dynamic and interactive net pages. The setTimeOut() and setInterval() features provide a easy strategy for scheduling code execution at a selected time or interval. These features can be utilized to make a slide present, animations, and different interactive components that should be up to date or modified over time on an online web page. 

Submit thumbnail generated with OpenAI’s DALL-E 2.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments