Underscore.js _.delay() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Much like setTimeout, invokes the function after wait milliseconds (official Underscore docs). Optional arguments are forwarded when it runs. Stable Function helper — not deprecated.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.delay. Start with this line.

Underscore
_.delay(function, wait, *arguments)
CallWhat it does
_.delay(function, wait, *arguments)Primary signature.
_.delay(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.delay(function, wait, *arguments)

Parameters

ParameterDescription
functionRequired. Function to run later
waitRequired. Delay in milliseconds
*argumentsOptional. Forwarded to the function when it runs

Return Value

Return ValueDescription
*Timer id (cancel with clearTimeout)

Underscore.js Examples

Example 1 — Basic Delay

Run a function once after a short wait.

Underscore
var log = [];

_.delay(function () {
  log.push("later");
}, 50);

// right away: log → []
// after ~50ms: log → ["later"]
Try It Yourself

How It Works

  1. _.delay schedules the push for 50 ms later.
  2. Sync code continues; log stays empty at first.
  3. Timer fires → "later" is pushed.
  4. Same idea as official “logged later” after 1s.

Example 2 — Forward Arguments (Official Idea)

Extra args after wait are passed when the function runs.

Underscore
var log = [];

_.delay(function (msg) {
  log.push(msg);
}, 50, "logged later");

// after ~50ms: log → ["logged later"]
Try It Yourself

How It Works

  1. First arg is the function; second is wait.
  2. "logged later" is stored for the call.
  3. When it runs, it becomes msg.
  4. Matches the official _.delay(log, 1000, …) pattern.

Example 3 — Cancel with clearTimeout

Save the timer id and cancel before the wait ends.

Underscore
var log = [];
var id = _.delay(function () {
  log.push("ran");
}, 80);

clearTimeout(id);
// log stays [] — delayed call never ran
Try It Yourself

How It Works

  1. _.delay returns a timer id.
  2. clearTimeout(id) removes the pending call.
  3. fn never runs — log stays empty.
  4. Useful if the user navigates away first.

Example 4 — Delay vs Defer

Chosen wait vs ASAP after the stack clears.

Underscore
// Timed UX (toast, auto-hide):
_.delay(hideToast, 2000);

// Yield / post-DOM ASAP:
_.defer(measureLayout);
// same idea as: _.delay(measureLayout, 1)

// Rule of thumb:
// delay → "after N milliseconds"
// defer → "as soon as the stack is free"
Try It Yourself

How It Works

  1. Delay needs an explicit wait.
  2. Defer ≈ _.delay(fn, 1).
  3. Timed UX → _.delay.
  4. UI yield → _.defer.

Browser & Runtime Support

_.delay ships with Underscore.js (1.x). It uses timers (setTimeout / clearTimeout) and runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.delay()

Available in [email protected] (UMD / ESM). Same timer model as setTimeout. Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.delay Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.delay(function, wait, *arguments) runs the function once after wait milliseconds — like setTimeout. Extra arguments are forwarded when it runs.
Same core job. _.delay puts the function first and forwards args: _.delay(fn, 1000, a, b) calls fn(a, b) after 1s. setTimeout is (fn, wait) and usually needs a wrapper for args.
Yes — a timer id (like setTimeout). Cancel with clearTimeout(id) before it fires.
Save the id from _.delay and call clearTimeout(id) before the wait ends.
_.defer(fn) is essentially _.delay(fn, 1) — yield ASAP. Use _.delay when you need a specific wait; _.defer when you just want the stack to clear.
No. Like setTimeout, this is not preserved. Bind first with _.bind or pass an already-bound function.

Next: _.defer()

Schedule a function as soon as the current call stack clears.

_.defer() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful