jQuery $.Callbacks() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Factory API

What You’ll Learn

The $.Callbacks() factory creates a managed list of functions you can register with add() and run with fire(). Optional flags control firing behavior. This tutorial covers syntax, all four flags, five worked examples, and how Callbacks power jQuery Deferred internally.

01

Factory

$.Callbacks()

02

once

Fire one time

03

memory

Remember args

04

unique

No duplicates

05

stopOnFalse

Break chain

06

Deferred

Internal use

Introduction

JavaScript apps constantly coordinate work through callbacks — functions passed to run later when data arrives, a user clicks, or a timer finishes. Scattering those functions across nested calls gets messy fast. jQuery’s Callbacks object gives you a reusable list with a small, predictable API.

The entry point is $.Callbacks(). It returns a fresh list you configure with optional flags, populate via add(), and trigger with fire(). jQuery itself uses this machinery inside Deferred for done, fail, and related queues.

Understanding the $.Callbacks() Method

$.Callbacks([flags]) is a factory function, not a method on an existing object. Each call produces an independent callback list with methods like add, remove, fire, lock, and disable.

Without flags, the list behaves simply: every fire() invokes all registered handlers in order. Flags tweak that behavior — run only once, recall last arguments for late subscribers, skip duplicate functions, or halt when a handler returns false.

💡
Beginner Tip

Pick flags when you create the list — you cannot change them afterward. Start with no flags for learning; add "once memory" when building plugin init hooks.

📝 Syntax

General form of the factory:

jQuery
jQuery.Callbacks( [ flags ] )

Parameters

  • flags (optional) — a string of space-separated behavior modifiers. Omit for default multi-fire behavior.

Available flags

FlagEffect
onceThe list fires at most one time; later fire() calls are ignored.
memoryRemembers the last fire() arguments; newly add()ed callbacks run immediately with those values.
uniqueIgnores attempts to add the same function reference twice.
stopOnFalseStops invoking remaining callbacks when one returns false.

Minimal example

jQuery
const callbacks = $.Callbacks();



callbacks.add(function () {

  console.log("Callback 1 executed");

});



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

⚡ Quick Reference

GoalCode
Default list$.Callbacks()
Fire only once$.Callbacks("once")
Late subscribers get last args$.Callbacks("memory")
Init hook (once + memory)$.Callbacks("once memory")
No duplicate handlers$.Callbacks("unique")
Breakable pipeline$.Callbacks("stopOnFalse")

📋 Default vs Flagged Lists

Choose flags based on how subscribers register and how often you fire.

Default
multi fire

Every fire runs all handlers

once
1× fire

Good for one-time setup

memory
replay args

Late add() still runs

unique
dedupe fn

Same reference added once

Examples Gallery

Each example demonstrates a different flag or default behavior. Use Try-it labs to run them in the browser.

📚 Getting Started

Create a list with no flags — the baseline behavior.

Example 1 — Default Callbacks List

No flags: handlers run on every fire(), and multiple add() calls build the queue.

jQuery
const 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

This is the behavior behind the add() tutorial: each fire() walks the entire current list.

Example 2 — The once Flag

After the first fire(), the list is spent — later fires do nothing.

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



callbacks.add(function () {

  console.log("Runs on first fire only");

});



callbacks.fire();

callbacks.fire(); // ignored — list already fired
Try It Yourself

How It Works

Ideal for “ready” or initialization events that should not repeat — similar to DOMContentLoaded firing once.

📈 Flag Behavior

Fine-tune registration timing and handler uniqueness.

Example 3 — The memory Flag

After fire(), new handlers added with add() run immediately with the remembered arguments.

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



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



callbacks.add(function (event, data) {

  console.log(event, "v" + data.version);

});

// → ready v1  (fires immediately on add)
Try It Yourself

How It Works

Combine with once as "once memory" for plugin ready callbacks — late-loading modules still receive the init payload exactly once.

Example 4 — The unique Flag

Adding the same function reference twice only stores it once.

jQuery
const 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

Two separate inline functions with identical bodies are still different references — unique dedupes by reference, not by source code.

Example 5 — The stopOnFalse Flag

When a callback returns false, remaining handlers are skipped — useful for filter or validation pipelines.

jQuery
const 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

Returning anything other than false continues the chain. This mirrors jQuery’s event system where false can cancel default actions.

🚀 Use Cases

  • Custom events — expose $.Callbacks() lists as onReady, onChange, or onDestroy hooks.
  • Modular development — decouple publishers (fire) from subscribers (add) inside a module.
  • Asynchronous coordination — queue steps that run when a long task completes.
  • Plugin extensibility — ship a plugin with internal Callbacks lists third parties can extend.
  • Deferred internals — understand how jQuery queues done/fail/progress handlers under the hood.

🧠 How $.Callbacks() Works

1

Factory call

$.Callbacks(flags) returns a new list object configured with your flags.

Create
2

Register

Call add() to queue handlers. With memory, late adds may fire instantly.

add()
3

Invoke

fire() or fireWith() runs handlers, honoring once and stopOnFalse.

fire()
=

Controlled callback flow

Predictable pub/sub without reinventing event lists in every plugin.

📝 Notes

  • Flags are fixed at creation — combine them in one string: "once memory unique".
  • memory without a prior fire() does nothing special on add().
  • unique compares function references, not logical equality.
  • Lists can be lock()ed or disable()d after creation — see those tutorials.
  • Deferred’s done queue uses Callbacks with "once memory" semantics internally.

Browser Support

$.Callbacks() was added in jQuery 1.7 and is available in all subsequent 1.x, 2.x, and 3.x releases wherever jQuery runs.

jQuery 1.7+

jQuery $.Callbacks()

Supported in all modern browsers and legacy IE when using a compatible jQuery build. Behavior is normalized by jQuery, not the host browser.

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

Bottom line: Safe in jQuery-based projects. Pick flags at creation time for the behavior you need.

🎉 Conclusion

$.Callbacks() is the factory at the heart of jQuery’s callback management. Master the default list first, then use flags — especially once and memory — for plugin-ready patterns and predictable event buses.

Explore add(), fire(), and disable() next to complete your Callbacks toolkit.

💡 Best Practices

✅ Do

  • Choose flags before exposing lists publicly
  • Use "once memory" for ready/init hooks
  • Name lists and handlers for readable debugging
  • Document which lists consumers may fire()
  • Test late-registration paths with memory

❌ Don’t

  • Expect flags to change after creation
  • Rely on unique to dedupe inline lambdas
  • Fire huge lists on every mousemove without throttling
  • Skip error handling inside plugin hooks
  • Reimplement Deferred when Callbacks + flags suffice

Key Takeaways

Knowledge Unlocked

Five things to remember about $.Callbacks()

The factory behind jQuery callback lists.

5
Core concepts
02

once

Single fire

Flag
💾 03

memory

Replay args

Late add
🔖 04

unique

Dedupe refs

Flag
05

stopOnFalse

Break chain

Filter

❓ 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: once (fire only one time), memory (remember last fire arguments), unique (ignore duplicate function references), and stopOnFalse (stop the chain when a callback returns false). Combine them, e.g. $.Callbacks("once memory").
A default list runs every handler on each fire(). A once list ignores subsequent fire() calls after the first successful invocation — useful for initialization hooks.
After fire() runs, memory stores the last arguments. Any callback added later is invoked immediately with those stored values — handy when subscribers register after an event already fired.
jQuery Deferred uses Callbacks lists internally for done, fail, progress, and always queues. $.Callbacks() exposes the same list machinery directly for custom event buses and plugin APIs.
If you already use jQuery plugins or legacy jQuery code, yes. For greenfield apps without jQuery, native EventTarget, mitt, or Promise patterns often suffice — but understanding Callbacks helps when reading jQuery source and plugins.
Did you know?

jQuery’s $.ajax() done and fail queues are Callbacks lists created with "once memory" — so a handler you attach after the request completes still runs with the response data.

Continue to disable()

Learn how to permanently stop a Callbacks list from firing.

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.

6 people found this page helpful