setTimeout() schedules a function to run once after a delay in milliseconds. It does not pause JavaScript execution. Instead, it registers a timer and lets the rest of your code continue running.
Use setTimeout() for delayed UI updates, retry logic, small wait periods, and scheduling work after the current call stack. Use clearTimeout() when the scheduled callback should be cancelled before it runs.
Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).
JavaScript setTimeout Syntax
The general form is setTimeout(callback, delay, arg1, arg2, …): after delay milliseconds, callback is invoked with the extra arguments. Here is a minimal runnable version (delay 0 queues the callback as soon as the event loop can run it):
function callback(arg1, arg2) {
console.log("args:", arg1, arg2);
}
const timeoutId = setTimeout(callback, 0, "first", "second");
console.log("timer id:", timeoutId != null);You should see the timer handle logged right away (timer id: true), then the callback runs on the next event-loop turn and prints args: first second. That order shows how synchronous code runs first while a delay-0 timer still waits for the stack to clear.
Method 1: Run Code After a Delay
This pattern wraps setTimeout in a Promise so callers can chain with .then() (or async/await elsewhere) without blocking the thread. In a normal script file you cannot use top-level await unless you are in an ES module; .then() keeps the example valid everywhere Node or the browser loads classic scripts.
function later(ms, value) {
return new Promise((resolve) => setTimeout(() => resolve(value), ms));
}
later(5, "done").then((value) => {
console.log("timeout-value:", value);
});You should see timeout-value: done after the short delay—here 5 ms so Run returns quickly, while the same structure works for longer UI or retry waits.
Method 2: Cancel setTimeout with clearTimeout()
setTimeout returns an opaque timer id (shape differs slightly between browsers and Node—treat it as a handle, not a number you rely on forever). Calling clearTimeout with that id cancels the pending callback if it has not run yet.
const id = setTimeout(() => {
console.log("will not run");
}, 50);
clearTimeout(id);
console.log("timeout-cleared:", true);You should see only timeout-cleared: true. The line will not run never appears in the console because the timer was cleared before the delay elapsed—useful when a user navigates away, a request finishes early, or debouncing logic decides the scheduled work is no longer needed.
Method 3: Pass Arguments to the Callback
The values after the delay are forwarded to the callback as arguments. In production you might use a larger delay (for example 1000 ms); here the delay is shortened so Run finishes quickly while the pattern stays the same.
setTimeout((name) => {
console.log(`Hello ${name}`);
}, 50, "Ana");Anything after the delay argument is forwarded positionally into your callback—handy when you already have the data at scheduling time and want to avoid a closure over outer variables.
You should see Hello Ana once the timer fires. A longer delay (for example 1000 ms for a one-second pause in UX copy) uses the same signature; the delay here is only shortened for a snappy Run experience.
Method 4: Avoid String Code in setTimeout
Pass a function (or arrow that calls your function). Do not pass strings such as setTimeout("runTask()", 1000) — that path is eval-like, harder to debug, and risky for injection.
function runTask() {
console.log("runTask executed");
}
setTimeout(() => runTask(), 50);Historically some code passed a string of JavaScript for the browser to eval later (setTimeout("runTask()", 1000)). That is harder to lint, breaks minification, and is a security footgun. Prefer a real function reference or an arrow that forwards to your named function, as above.
You should see runTask executed after the short delay—the same wiring works whether runTask lives in the same file or is imported from a module.
Common Questions About setTimeout in JavaScript
Does setTimeout stop JavaScript execution?
No. setTimeout() is asynchronous. The remaining code continues to run while the timer is waiting.
What unit does setTimeout use?
The delay is in milliseconds. 1000 means about one second.
What is the difference between setTimeout and setInterval?
setTimeout() runs once after a delay. setInterval() runs repeatedly until cancelled.
Summary
setTimeout() is the standard JavaScript timer for running a callback once after a delay. It returns a timer ID that can be passed to clearTimeout(). It does not block execution, and real delays can be longer than requested depending on runtime and browser scheduling.

