Javascript
May 3, 2026
Spot the bug: This should only run the callback once after 1 second.
function delay(ms, callback) {
setInterval(callback, ms)
}
delay(1000, () => console.log("done"))
Explanation
setInterval keeps calling the function over and over every ms milliseconds until you stop it. setTimeout runs the function just once after the delay. Easy trick to remember: Interval = repeats (like an interval in sports), Timeout = runs once after a delay.
📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.