Day 44 of #100daysofjs

setInterval and setTimeout in JavaScript

In JavaScript, there are two functions that are commonly used to control the timing of events: setInterval and setTimeout. Both of these functions can be used to delay the execution of a piece of code or to repeat a piece of code at a regular interval. Let's take a closer look at each of these functions.

setTimeout

setTimeout is used to execute a function after a specified amount of time has passed. The syntax for setTimeout is as follows:

setTimeout(function, delay);
  • The function parameter is the function that you want to execute.

  • The delay parameter is the number of milliseconds to wait before executing the function. Here's an example that demonstrates how to use setTimeout:

setTimeout(function() {  alert('Hello, world!');}, 1000);

This code will wait for 1000 milliseconds (1 second) and then display an alert with the message "Hello, world!".

clearTimeout

If you change your mind and want to cancel the execution of the function before the specified delay has passed, you can use the clearTimeout function. Here's how it works:

var timeoutID = setTimeout(function() {  alert('This message will never be displayed.');}, 1000); clearTimeout(timeoutID);

In this example, the setTimeout function is called to display an alert after 1000 milliseconds. However, before the 1000 milliseconds have passed, the clearTimeout function is called to cancel the execution of the function. As a result, the alert message will never be displayed.

setInterval

setInterval is used to execute a function repeatedly at a fixed interval. The syntax for setInterval is as follows:

setInterval(function, delay);
  • The function parameter is the function that you want to execute.

  • The delay parameter is the number of milliseconds to wait before executing the function again. Here's an example that demonstrates how to use setInterval:

var count = 0;var intervalID = setInterval(function() {  count++;  console.log('Count: ' + count);}, 1000);

This code will increment the count variable every second and log the current value of count to the console.

clearInterval

If you want to stop the execution of the function that is being repeatedly executed using setInterval, you can use the clearInterval function. Here's how it works:

var intervalID = setInterval(function() {  console.log('This message will be displayed every second.');}, 1000); clearInterval(intervalID);

In this example, the setInterval function is called to log a message to the console every second. However, before the function can be executed more than once, the clearInterval function is called to stop the execution of the function.

Difference between setInterval() and setTimeout()

The main difference between setInterval() and setTimeout() is that setInterval() will execute the block of code repeatedly after a fixed delay whereas setTimeout() will only execute the block of code once after a specified delay.

Did you find this article valuable?

Support Arpan Mukherjee by becoming a sponsor. Any amount is appreciated!