jQuery $.Callbacks() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is $.Callbacks()?

$.Callbacks() (alias jQuery.Callbacks()) is a factory that creates a callback list — since jQuery 1.7. The list manages handlers with add(), remove(), fire(), and more. Optional flags change how the list behaves. jQuery uses the same machinery inside $.ajax() and Deferred.

Remember
var cbs = $.Callbacks()                 // default (multi-fire)
var cbs = $.Callbacks("once")           // fire once
var cbs = $.Callbacks("memory")         // replay last args on late add
var cbs = $.Callbacks("unique")         // no duplicate refs
var cbs = $.Callbacks("stopOnFalse")    // halt when fn returns false
var cbs = $.Callbacks("once memory")    // combine flags (Deferred-style)

cbs.add(fn) → queue
cbs.fire(args…) → run handlers
NeedUse
Create a list$.Callbacks([flags])
Register handlerscbs.add(fn)
Run handlerscbs.fire(args…)
Unsubscribecbs.remove(fn)

How to Use $.Callbacks()

Pick flags up front, create the list, then register and fire.

  1. Decide behavior: default multi-fire, or flags like once / memory.
  2. Call $.Callbacks() or $.Callbacks("once memory").
  3. Register with add(), trigger with fire() / fireWith().
  4. Clean up with remove(), empty(), lock(), or disable() as needed.
PieceRequired?Role
flags stringOptionalSpace-separated behavior modifiers
Return value—Callbacks object (add, fire, …)
Since—jQuery 1.7

Pseudocode

Pseudocode
list = empty queue
flags = parse space-separated string
memoryArgs = none
fired = false

add(fn):
  if unique and fn already in list: skip
  append fn
  if memory and fired: call fn(memoryArgs) now

fire(args…):
  if once and fired: return
  fired = true
  if memory: memoryArgs = args
  for each fn in list:
    result = fn(args…)
    if stopOnFalse and result === false: break

Cheat Sheet

Flags and the methods you use most often.

TopicDetail
Syntax$.Callbacks( [flags] )
onceOnly the first fire() runs
memoryLate add() gets last fire args immediately
uniqueSame function reference added only once
stopOnFalseReturning false stops remaining handlers
Combine"once memory", "unique memory", …
Deferred tip.done() / .fail() use "once memory"

Live Preview

Pick a flag mode and run a short add / fire sequence against a fresh list.

Browser demo of $.Callbacks flags — same ideas as the examples below.

Result
$.Callbacks()
add(fn1); fire() → fn1
add(fn2); fire() → fn1, fn2

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Default Callbacks List

No flags: every fire() runs all handlers currently in the list.

jQuery
var callbacks = $.Callbacks();

callbacks.add(function () {
  console.log("Callback 1 executed");
});

callbacks.fire();
// → Callback 1 executed

callbacks.add(function () {
  console.log("Callback 2 executed");
});

callbacks.fire();
// → Callback 1 executed
// → Callback 2 executed
Try It Yourself

How It Works

  1. Create. $.Callbacks() starts an empty multi-fire list.
  2. First fire. Only handler 1 exists → one log line.
  3. Second add. Handler 2 joins the same list.
  4. Second fire. Both handlers run — the first is not cleared.

Example 2 — The once Flag

After the first fire(), later fires are ignored.

jQuery
var callbacks = $.Callbacks("once");

callbacks.add(function () {
  console.log("Runs on first fire only");
});

callbacks.fire();
callbacks.fire(); // ignored
Try It Yourself

How It Works

  1. Flag at creation. "once" marks the list as single-shot.
  2. First fire. The handler runs normally.
  3. Second fire. The list is spent → no output.
  4. Use case. Init / ready hooks that must not repeat.

Example 3 — The memory Flag

After fire(), a late add() runs immediately with the remembered arguments.

jQuery
var callbacks = $.Callbacks("memory");

callbacks.fire("ready", { version: 1 });

callbacks.add(function (event, data) {
  console.log(event, "v" + data.version);
});
// → ready v1  (runs on add)
Try It Yourself

How It Works

  1. Fire first. No handlers yet — args are stored because of memory.
  2. Late add. The new handler runs right away with "ready" and the object.
  3. No second fire needed. Memory replays the last payload on subscribe.
  4. Pair with once. "once memory" matches Deferred done/fail queues.

Example 4 — The unique Flag

Adding the same function reference twice only stores it once.

jQuery
var callbacks = $.Callbacks("unique");

function onChange() {
  console.log("Changed");
}

callbacks.add(onChange);
callbacks.add(onChange); // ignored — same reference

callbacks.fire(); // → Changed (once)
Try It Yourself

How It Works

  1. Named function. Keep a stable reference so unique and remove() can match it.
  2. First add. onChange is stored.
  3. Second add. Same reference → skipped.
  4. Caveat. Two separate inline functions are different refs — both would be kept.

Example 5 — The stopOnFalse Flag

When a callback returns false, remaining handlers are skipped.

jQuery
var callbacks = $.Callbacks("stopOnFalse");

callbacks.add(function () {
  console.log("Step 1: pass");
});

callbacks.add(function () {
  console.log("Step 2: block");
  return false;
});

callbacks.add(function () {
  console.log("Step 3: never reached");
});

callbacks.fire();
Try It Yourself

How It Works

  1. Step 1. Returns nothing → chain continues.
  2. Step 2. Returns false → stop signal.
  3. Step 3. Never called.
  4. Use case. Validation pipelines where a failed check cancels the rest.

Edge Cases & Pitfalls

Check these before choosing flags for a real list.

flags

Flags are immutable

Set them only in $.Callbacks(flags). Wrong flags? Create a new list.

default

Default lists re-fire everyone

A second fire() runs all current handlers again — use once if that is wrong for your case.

unique

unique is by reference

Two anonymous functions with identical bodies are still different objects — both get added.

memory

Empty fire still sets memory

Firing with no handlers still stores args when memory is on — late adds will run with those args.

stop

Only strict false stops

stopOnFalse reacts to return false. Returning 0, null, or undefined does not stop the chain.

Browser Support

$.Callbacks() is part of jQuery core (since 1.7). It works in any browser where your jQuery build runs — the list logic lives in jQuery, not as a separate browser API.

✓ jQuery 1.7+ · Browsers

jQuery $.Callbacks()

Supported in jQuery 1.x, 2.x, and 3.x across Chrome, Firefox, Safari, Edge, and other browsers with jQuery loaded.

100% With jQuery loaded
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.Callbacks() Browsers

Bottom line: Safe wherever jQuery is loaded. Choose flags at creation, then use add() / fire() for the lifecycle.

Performance Notes

OperationCostBest for
$.Callbacks()O(1) setupCreating a list once per hook
add / fireO(1) / O(n)Register vs invoke all handlers
unique addO(n) scanPreventing duplicate refs

Creating a Callbacks object is cheap. Cost shows up when you fire() many heavy handlers. Keep callbacks light, prefer named refs for remove(), and avoid recreating lists on every event.

Key Takeaways

  • Factory: $.Callbacks() builds a managed handler list.
  • Flags: once, memory, unique, stopOnFalse — set at creation only.
  • Lifecycle: add to subscribe, fire to publish.
  • Deferred link: done/fail queues use "once memory" under the hood.

One line: pick flags first, then add / fire — that is the whole Callbacks model.

Frequently Asked Questions

$.Callbacks() (same as jQuery.Callbacks()) is a factory that creates a callback list — an object with add(), remove(), fire(), and related methods. Use it to manage groups of functions that should run together when an event or async step completes.
Optional space-separated strings passed at creation: once (fire only one time), memory (remember last fire args and replay them for late add()), unique (ignore duplicate function references), and stopOnFalse (stop the chain when a callback returns false). Combine them, e.g. $.Callbacks("once memory").
No. Flags are set only in the $.Callbacks(flags) call. Create a new list if you need different behavior.
After fire() runs, memory stores the last arguments. Any callback added later is invoked immediately with those stored values — the same idea Deferred uses for .done() handlers registered after resolve.
jQuery Deferred uses Callbacks lists internally for done, fail, and progress queues — typically with "once memory". $.Callbacks() exposes that same list machinery for custom hooks and plugin APIs.
Yes if you already ship jQuery plugins or need a small pub/sub inside a jQuery codebase. For greenfield apps without jQuery, native EventTarget or Promises often suffice — but Callbacks is still useful for reading jQuery internals.

Next: disable()

Learn how to shut down a Callbacks list so fire() becomes a no-op.

disable() 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