#Javascript
02/02/2021

setTimeout vs setInterval

setTimeout is a function that executes, code, or functions once after a specified period of time in milliseconds.

for example:

setTimeout(function () {
    console.log('Done after one second!');
}, 1000);

setInterval is a function that executes code or functions cyclically every specified period, starting after the first period. The function returns an identifier that can be used for logging operations with clearInterval.

const intervalID = setInterval(function () {
     console.log('Done every second...');
}, 1000);

// clearInterval(intervalID);  

In conclusion, setTimeout() triggers the expression only once while setInterval() keeps triggering expression regularly after the given interval of time. (unless you tell it to stop).

To stop further calls, we can call clearInterval() or clearTimeout() methods like in the above example.

setTimeout vs setInterval

Blog

© Copyright 2025 | CristianBernal All Rights Reserved