close

explicit-timer-delay

Unreleased

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/explicit-timer-delay': 'error',
    },
  },
]);

Rule Details

Require an explicit delay for global setTimeout() and setInterval() calls. The rule also recognizes calls through window, globalThis, global, and self, when the global is enabled and not shadowed by a local declaration.

Examples of incorrect code with the default options:

setTimeout(callback);
globalThis.setInterval(callback);

Examples of correct code:

setTimeout(callback, 0);
globalThis.setInterval(callback, 1000);

Optional calls, computed method names, locally declared timer functions, calls without arguments, and a spread first argument are not reported.

Options

Type: "always" | "never"

Default: "always"

"always" requires a delay when a timer has exactly one non-spread argument. Its automatic fix adds , 0 after the complete callback argument, preserving parentheses and any existing trailing comma.

"never" disallows an explicit zero delay when there are exactly two arguments. Non-zero delays and calls with additional callback arguments remain allowed. Signed and parenthesized zero literals are recognized.

{
  'unicorn/explicit-timer-delay': ['error', 'never']
}

With "never", setTimeout(callback, 0) becomes setTimeout(callback).

Differences from upstream

Calls with a spread first argument are left unchanged in both modes. In "never" mode, upstream v75.0.0 can mistake a later zero for the delay even when the spread already supplies the callback and delay:

const args = [callback, 1000];
setTimeout(...args, 0);

Here 0 is passed to the callback. Removing it changes behavior, so this rule does not report the call. Pass the callback and delay explicitly to make their positions clear and enable checking.

Original Documentation