setTimeout and setInterval in JavaScriptJavaScript allows us to perform tasks after a certain time has passed or to repeat tasks continuously at regular intervals. To do this, we use two special functions: setTimeout and setInterval.
setTimeout: Delay a TaskThe setTimeout function allows you to delay the execution of a task (or function) by a specified amount of time. Think of it as setting an alarm: the task won’t happen immediately, but it will happen after the time you set has passed.
setTimeout Work?Imagine you want something to happen after 3 seconds, like showing a message to the user or changing the color of an element. You can use setTimeout to wait for 3 seconds and then perform the action.
setTimeout(function, delay);
Let’s break this down:
function: This is the code (a function) you want to run after the delay.delay: This is the time (in milliseconds) to wait before running the function. 1 second = 1000 milliseconds.setTimeoutHere’s an example where we use setTimeout to display a message after 2 seconds:
console.log("Wait for 2 seconds...");
setTimeout(function() {
console.log("2 seconds have passed!");
}, 2000);
In this example:
console.log runs immediately, displaying "Wait for 2 seconds...".setTimeout waits for 2 seconds (2000 milliseconds) before running the second console.log that displays "2 seconds have passed!".This shows how setTimeout delays a task without stopping the rest of the code from running. The delayed task runs later, once the timer is up.
setInterval: Repeat a TaskNow, what if you want something to happen over and over, continuously, after a certain time interval? That’s where setInterval comes in. It’s like setting an alarm that keeps ringing every 5 minutes until you stop it.
setInterval Work?While setTimeout runs a task only once after a delay, setInterval keeps running the task over and over at a specified interval. This is useful if you want to update something regularly, like the time on a clock, or if you want to continuously check something, like new notifications.
setInterval(function, interval);
Let’s break this down:
function: This is the code (a function) you want to run repeatedly.interval: This is the time (in milliseconds) between each repetition of the function.setIntervalHere’s an example where we use setInterval to display a message every 3 seconds:
console.log("Starting...");
setInterval(function() {
console.log("3 seconds have passed!");
}, 3000);
In this example:
console.log runs immediately, displaying "Starting...".setInterval runs the second console.log that displays "3 seconds have passed!".This shows how setInterval repeats a task at regular intervals. It doesn’t stop by itself—you need to manually stop it if you want it to end.
setInterval and setTimeoutIf you set up an interval or a timeout but want to stop it before it finishes, you can use the clearTimeout and clearInterval functions.
setIntervalTo stop an interval, you need to save it to a variable and then use clearInterval to stop it. Let’s see how:
let intervalId = setInterval(function() {
console.log("Repeating every 2 seconds");
}, 2000);
// After 6 seconds, stop the interval
setTimeout(function() {
clearInterval(intervalId);
console.log("Interval stopped");
}, 6000);
In this example:
setTimeout to stop the interval after 6 seconds using clearInterval(intervalId).setTimeout and setIntervalJust like in other functions, you can use arrow functions to shorten the code when using setTimeout and setInterval. Here’s how you can rewrite the examples above with arrow functions:
setTimeout:
setTimeout(() => {
console.log("2 seconds have passed!");
}, 2000);
setInterval:
let intervalId = setInterval(() => {
console.log("Repeating every 3 seconds");
}, 3000);
setTimeout(() => {
clearInterval(intervalId);
console.log("Interval stopped");
}, 9000);
Now that you understand setTimeout and setInterval, here are some practice tasks to try on your own:
setTimeout to display a message after 5 seconds.setInterval to show the current time every second.