JavaScript provides timing functions that let you run code after a delay or repeatedly at a fixed interval. These are commonly used for animations, scheduled tasks, countdowns, notifications, and small delays in user interfaces.
The main timing functions are setTimeout(), clearTimeout(), setInterval(), and clearInterval().
setTimeout(fn, delay) – runs a function once after a given delay (in milliseconds).clearTimeout(id) – cancels a timeout created by setTimeout().setInterval(fn, delay) – runs a function repeatedly every given delay.clearInterval(id) – stops an interval created by setInterval().setTimeout() and setInterval() return an ID that you must store to cancel them later.| Function | Purpose | Runs |
|---|---|---|
| setTimeout() | Delay execution | Once after delay |
| clearTimeout() | Cancel timeout | Stops scheduled run |
| setInterval() | Repeat execution | Repeatedly |
| clearInterval() | Stop interval | Stops repeating |
Basic syntax for JavaScript timing functions:
setTimeout(callback, delayInMs, arg1, arg2, ...)setInterval(callback, delayInMs, arg1, arg2, ...)clearTimeout(timeoutId)clearInterval(intervalId)Timing in JavaScript is not exact. If the main thread is busy, calls may be delayed. Use timing functions for scheduling, animations, UI effects, and background polling—but not for ultra-precise timing.
<script>
setTimeout(function() {
alert("This message is shown after 3 seconds");
}, 3000);
</script>
<script>
function greet() {
console.log("Hello after 2 seconds");
}
setTimeout(greet, 2000);
</script>
<script>
let timer = setTimeout(() => {
console.log("This will not run");
}, 4000);
clearTimeout(timer);
</script>
<script>
let count = 0;
let intervalId = setInterval(() => {
count++;
console.log("Count: " + count);
if (count === 5) {
clearInterval(intervalId); // stop after 5 times
}
}, 1000);
</script>
<script>
let timer = setInterval(() => {
console.log("This runs every 2 seconds");
}, 2000);
setTimeout(() => {
clearInterval(timer);
console.log("Interval stopped after 6 seconds");
}, 6000);
</script>
"This message is shown after 3 seconds"."Hello after 2 seconds" is logged to the console once, after 2 seconds.Count: 1, Count: 2, ... up to Count: 5, once per second, then stops."This runs every 2 seconds" repeatedly, and after 6 seconds it logs "Interval stopped after 6 seconds" and stops the interval.Click the button to start a demo interval that counts up every second and stops at 5.
Current count: 0
This usessetInterval() and clearInterval() behind the scenes.setTimeout() for one-time delays such as showing a welcome message or hiding a popup.setInterval() for repeated actions like timers, clocks, and polling APIs.setTimeout() or setInterval() so you can cancel it later.setTimeout() (like "alert('hi')"); always pass a function instead.setInterval(), and stop when it reaches 0.setTimeout() when the page loads.setInterval().setTimeout() calls.setTimeout() and clearTimeout().setInterval().setInterval() and stop after 3 notifications.<div> every half second for 5 seconds.