Skip to content
← Back to rules

unicorn/explicit-timer-delay Style

🛠️ An auto-fix is available for this rule.

What it does

Enforce or disallow explicit delay argument for setTimeout() and setInterval().

Why is this bad?

When using setTimeout() or setInterval(), the delay parameter is optional and defaults to 0. This rule allows you to enforce whether the delay argument should always be explicitly provided or omitted when it's 0.

Examples

Examples of incorrect code for this rule:

javascript
setTimeout(() => console.log("Hello"));
setInterval(callback);
window.setTimeout(() => console.log("Hello"));
globalThis.setInterval(callback);

Examples of correct code for this rule:

javascript
setTimeout(() => console.log("Hello"), 0);
setInterval(callback, 0);
window.setTimeout(() => console.log("Hello"), 0);
globalThis.setInterval(callback, 0);
setTimeout(() => console.log("Hello"), 1000);
setInterval(callback, 100);

With the "never" option, explicit 0 delays are disallowed and non-zero delays are still allowed.

Examples of incorrect code for the "never" option:

javascript
setTimeout(() => console.log("Hello"), 0);
setInterval(callback, 0);
window.setTimeout(() => console.log("Hello"), 0);
globalThis.setInterval(callback, 0);

Examples of correct code for the "never" option:

javascript
setTimeout(() => console.log("Hello"));
setInterval(callback);
window.setTimeout(() => console.log("Hello"));
globalThis.setInterval(callback);
setTimeout(() => console.log("Hello"), 1000);
globalThis.setInterval(callback, 100);

Configuration

This rule accepts one of the following string values:

"always"

Require explicit delay argument for clarity.

"never"

Disallow explicit 0 delay, prefer implicit default.

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/explicit-timer-delay": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/explicit-timer-delay": "error",
  },
});
bash
oxlint --deny unicorn/explicit-timer-delay

Version

This rule was added in vnext.

References