$.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
Call
What it does
flags string
Space-separated behavior modifiers
Return value
Callbacks object (add, fire, …)
Since
jQuery 1.7
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Decide behavior: default multi-fire, or flags like once / memory.
Step 2
Call $.Callbacks() or $.Callbacks("once memory").
Step 3
Register with add(), trigger with fire() / fireWith().
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters 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.
Create.$.Callbacks() starts an empty multi-fire list.
First fire. Only handler 1 exists → one log line.
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
Fire first. No handlers yet — args are stored because of memory.
Late add. The new handler runs right away with "ready" and the object.
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.