unicorn/explicit-timer-delay
Require or disallow an explicit delay for setTimeout() and setInterval()
fixable
setTimeout(callback) runs the callback as soon as it can, as if the delay were 0. The reader can't tell whether that's on purpose or you forgot the delay. For setInterval() a forgotten delay means a callback that runs over and over without a pause. Writing 0 shows that you meant it.
The rule also checks the timers on window, globalThis, self and global.
--fix adds , 0. With "never", it removes the 0.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/explicit-timer-delay" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"unicorn/explicit-timer-delay" = ["error", "always"] # "always" or "never"Examples#
example.js2 problems
setTimeout(() => input.focus())setTimeout should have an explicit delay argument.;
window.setInterval(poll)setInterval should have an explicit delay argument.;example.jsNo problems
setTimeout(() => input.focus(), 0);
window.setInterval(poll, 1000);Options#
| Option | What it does |
|---|---|
"always" (default) | Report a timer without a delay. |
"never" | Report a delay of 0, which is the same as no delay. Other delays are fine. |
example.jswith
"never"1 problemsetTimeout(() => input.focus(), 0setTimeout should not have an explicit delay of 0.);example.jswith
"never"No problemssetTimeout(() => input.focus());
setTimeout(() => input.blur(), 200);