jQuery $.Callbacks() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Callbacks

Definition

$.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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
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
CallWhat it does
flags stringSpace-separated behavior modifiers
Return valueCallbacks object (add, fire, …)
SincejQuery 1.7

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Decide behavior: default multi-fire, or flags like once / memory.
Step 2Call $.Callbacks() or $.Callbacks("once memory").
Step 3Register with add(), trigger with fire() / fireWith().

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: Deferred Methods

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

Deferred Methods tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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