Underscore.js _.debounce() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Creates a debounced version of the function that postpones execution until after wait milliseconds have elapsed since the last invocation (official Underscore docs). Useful after input stops (search, resize, Markdown preview). Pass true for immediate to fire on the leading edge. 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 _.debounce. Start with this line.

Underscore
_.debounce(function, wait, [immediate])
CallWhat it does
_.debounce(function, wait, [immediate])Primary signature.
_.debounce(...)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
_.debounce(function, wait, [immediate])

Parameters

ParameterDescription
functionRequired. Function to debounce
waitRequired. Quiet period in milliseconds
immediateOptional. true → leading edge instead of trailing

Return Value

Return ValueDescription
*Debounced function (+ .cancel())

Underscore.js Examples

Example 1 — Trailing (Default)

Many rapid calls → one run after quiet, with the last args.

Underscore
var log = [];
var save = _.debounce(function (q) {
  log.push(q);
}, 50);

save("a");
save("ab");
save("abc");
// after ~50ms of quiet: log → ["abc"]
Try It Yourself

How It Works

  1. Each save resets the 50 ms timer.
  2. No pause between calls → nothing runs yet.
  3. After quiet, one call runs with "abc".
  4. Same idea as official calculateLayout after resize stops.

Example 2 — Leading Edge (immediate)

Fire once immediately; ignore spam until quiet again.

Underscore
var log = [];
var click = _.debounce(function () {
  log.push("submit");
}, 100, true);

click();
click();
click();
// log → ["submit"] right away
// further clicks ignored until 100ms quiet
Try It Yourself

How It Works

  1. immediate: true runs on the first call.
  2. Extra clicks during the wait are ignored.
  3. Docs use case: prevent double-submit.
  4. After quiet, the next first click can fire again.

Example 3 — .cancel()

Clear a scheduled trailing call (teardown / navigate away).

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

later();
later.cancel();
// log stays [] — pending call cleared
Try It Yourself

How It Works

  1. later() schedules a trailing run.
  2. .cancel() clears that timer.
  3. fn never runs — log stays empty.
  4. Official tip: cancel on teardown when needed.

Example 4 — Debounce vs Throttle (Idea)

Same noisy stream, different strategies.

Underscore
// Typing in a search box:
_.debounce(search, 300);   // query after user pauses

// Tracking scroll position:
_.throttle(onScroll, 100); // update ~every 100ms while scrolling

// Rule of thumb:
// debounce → "when things settle"
// throttle → "while things keep happening"
Try It Yourself

How It Works

  1. Debounce collapses a burst into one settled call.
  2. Throttle samples during the burst.
  3. Search / resize / preview → debounce (official examples).
  4. Scroll / mousemove → usually _.throttle.

Browser & Runtime Support

_.debounce 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

_.debounce()

Available in [email protected] (UMD / ESM). Supports immediate and .cancel(). 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
_.debounce 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

_.debounce(function, wait, [immediate]) returns a wrapper that postpones running until wait milliseconds have elapsed since the last invocation. Official use: _.debounce(calculateLayout, 300) on resize.
Debounce waits for a quiet period, then runs once. Throttle runs at most once per wait while events keep coming. Use debounce for 'after typing stops'; throttle for continuous scroll/move updates.
Pass true to fire on the leading edge (first call), then ignore further calls until wait ms of quiet. Useful for preventing double-submit clicks.
At the end of the wait, the function is called with the arguments from the most recent invocation of the debounced wrapper.
Yes. Call .cancel() on the debounced function to clear any scheduled trailing invocation.
In trailing mode the real call is async, so you usually ignore the sync return. In immediate mode the leading call’s return value is available.

Next: _.once()

Create a version of a function that can only be called one time.

_.once() 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