jQuery Callbacks

Beginner
⏱️ 14 min read
📚 Updated: Jul 2026
🎯 12 Tutorials
Callback lists

What You’ll Learn

Need a lightweight pub/sub system or plugin hook list? jQuery Callbacks gives you a managed queue of functions — register with add(), trigger with fire(), and control lifecycle with flags and lock methods. This hub links to 12 Callbacks method tutorials.

01

$.Callbacks()

Create lists

02

add / remove

Subscribe

03

fire

Invoke

04

Flags

once memory

05

lock

Freeze list

06

12 guides

Full index

Introduction

Multiple parts of your app may need to react to the same event — data loaded, plugin initialized, button clicked. Instead of one giant function, you maintain a list of handlers and run them together when something happens.

What Is the jQuery Callbacks Object?

$.Callbacks() is a factory that returns a callback list — an object with methods like add(), fire(), remove(), and lock(). It separates registration from invocation, keeping event wiring clean and testable.

💡
Beginner Tip

Think of Callbacks as a mailing list: add() subscribes addresses, fire() sends the newsletter, and remove() unsubscribes one recipient.

Key Features

  • List management — Add, remove, or clear handlers in a controlled queue.
  • Flexible invocation — Pass arguments with fire() or bind this with fireWith().
  • Behavior flags — Optional once, memory, unique, and stopOnFalse at creation time.
  • Lifecycle control — Freeze with lock(), shut down with disable(), and query status with fired(), locked(), disabled().

📝 Syntax

Minimal Callbacks workflow:

jQuery
const callbacks = $.Callbacks();

callbacks.add(function (message) {
  console.log("Handler:", message);
});

callbacks.fire("Hello, Callbacks!");

Method groups

GroupExamplesPurpose
Factory$.Callbacks(), $.Callbacks("once")Create a new list with optional flags
List managementadd(), remove(), empty(), has()Register, unregister, clear, check membership
Invokefire(), fireWith()Run all handlers with args or custom this
Statusfired(), locked(), disabled()Read-only boolean checks
Controllock(), disable()Freeze list or shut down firing

🏷️ Callbacks Flags

Pass a space-separated string when creating the list:

FlagEffect
onceOnly the first fire() runs handlers; later fires are ignored.
memoryRemember last fire args; late add() handlers run immediately with stored values.
uniqueIgnore duplicate function references on add().
stopOnFalseStop invoking remaining handlers when one returns false.
jQuery
const onReady = $.Callbacks("once memory");
onReady.fire("app-ready");
onReady.add(function (state) {
  console.log("Late subscriber:", state); // replays "app-ready"
});

⚡ Quick Reference

GoalMethod
Create a list$.Callbacks()
Register handlercallbacks.add(fn)
Invoke handlerscallbacks.fire(args...)
Custom thiscallbacks.fireWith(ctx, [args])
Unsubscribe onecallbacks.remove(fn)
Clear all handlerscallbacks.empty()
Freeze list (no add/remove)callbacks.lock()
Shut down listcallbacks.disable()

When to Use Callbacks

  • Custom event buses — Lightweight pub/sub without DOM events.
  • Plugin hooks — Let users register callbacks for lifecycle events.
  • Deferred internals — jQuery uses Callbacks lists inside Deferred/promise queues.
  • Init guards — Combine once and fired() for one-time setup.
  • Test doubles — Spy on whether handlers were added or lists fired.

👀 Callbacks vs Deferred

Callbacks manages lists; Deferred manages async outcomes built on those lists:

Callbacks → add(fn) + fire(data) → you control the queue directly Deferred → done(fn) + resolve(data) → built-in pending/resolved/rejected states $.ajax() → returns jqXHR (Deferred) → Callbacks power under the hood

Callbacks Method Tutorial Index

Search by method name or browse by category. Each tutorial includes syntax, five try-it examples, and FAQs.

Getting Started

3 tutorials

Create Callbacks lists, register handlers, and invoke them.

MethodDescriptionTutorial
jQuery.Callbacks()Factory that creates a managed callback list with optional once, memory, unique, and stopOnFalse flags.Open
add()Register one or more handler functions on the list.Open
fire()Invoke all handlers with optional arguments.Open

List Management

3 tutorials

Unregister handlers, clear the list, and check membership.

MethodDescriptionTutorial
remove()Unregister specific handler references from the list.Open
empty()Remove every handler from the list in one call.Open
has()Check whether a specific function reference is registered.Open

Invoke & Status

2 tutorials

Fire with custom context and check whether the list has fired.

MethodDescriptionTutorial
fireWith()Invoke handlers with a custom this context and argument array.Open
fired()Return true if the list has been invoked at least once.Open

Control

4 tutorials

Freeze the handler set, shut down firing, and read status flags.

MethodDescriptionTutorial
disable()Permanently shut down the list — fire() becomes a no-op.Open
disabled()Return true if disable() was called on the list.Open
lock()Freeze the list — add(), remove(), and empty() stop working.Open
locked()Return true if lock() was called on the list.Open

Examples Gallery

Include jQuery 3.7+ and open DevTools Console (F12) to run each snippet.

📚 Create & Invoke

The core add-then-fire pattern.

Example 1 — Create a List and Fire Handlers

Register two handlers and invoke them with one fire() call.

jQuery
const callbacks = $.Callbacks();

callbacks.add(function (msg) {
  console.log("First:", msg);
});
callbacks.add(function (msg) {
  console.log("Second:", msg.toUpperCase());
});

callbacks.fire("hello");
add() Tutorial

How It Works

Handlers run in registration order. Each receives the arguments passed to fire().

Example 2 — Mini Event Bus

Wrap Callbacks in subscribe / emit methods for a simple pub/sub API.

jQuery
const EventBus = (function () {
  const listeners = $.Callbacks();
  return {
    on: function (fn) { listeners.add(fn); },
    emit: function (payload) { listeners.fire(payload); }
  };
})();

EventBus.on(function (data) {
  console.log("Received:", data.id, data.value);
});
EventBus.emit({ id: 42, value: "temperature" });
fire() Tutorial

How It Works

This pattern scales to off() with remove() and guards with has() — see those method tutorials for unsubscribe logic.

📈 Flags & Control

Behavior flags and lifecycle methods.

Example 3 — once Flag

Handlers run only on the first fire().

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

callbacks.add(function () { console.log("Runs once"); });
callbacks.fire();
callbacks.fire(); // ignored

How It Works

Combine with fired() to check whether init already ran. Flags are set at factory time, not on individual methods.

Example 4 — fireWith() for Custom this

Handlers read properties from a plugin instance passed as context.

jQuery
const hooks = $.Callbacks();
const plugin = { name: "Slider" };

hooks.add(function (value) {
  console.log(this.name + " changed to", value);
});

hooks.fireWith(plugin, [42]);

How It Works

Use fire() when default this is fine; use fireWith(ctx, [args]) when handlers behave like methods on an object.

Example 5 — lock() vs disable()

Locked lists still fire; disabled lists do not.

jQuery
const locked = $.Callbacks();
locked.add(function () { console.log("locked: fired"); });
locked.lock();
locked.fire(); // → locked: fired

const disabled = $.Callbacks();
disabled.add(function () { console.log("disabled: fired"); });
disabled.disable();
disabled.fire(); // nothing
lock() Tutorial

How It Works

lock() blocks add/remove; disable() blocks fire. Pick the control that matches your lifecycle needs.

🚀 Use Cases

  • Plugin lifecycle hooks — onInit, onDestroy callbacks users can register.
  • Internal event buses — Decouple modules without DOM event overhead.
  • Animation sequences — Fire when steps complete; remove one-time listeners.
  • Deferred queues — jQuery uses Callbacks for done/fail/always internally.

🌟 Advantages

  • Clean separation — Register handlers separately from triggering them.
  • Composable flags — Tune behavior with once, memory, unique, stopOnFalse.
  • Lifecycle hooks — lock, disable, and status checks for robust APIs.
  • Lightweight — No DOM required — pure JavaScript callback management.

💬 Usage Tips

  • Store handler references — Required for remove() and has().
  • Use empty() for bulk clear — Not remove() with no arguments.
  • Pair action/status methods — lock/locked, disable/disabled, fire/fired.
  • Lock after init — Prevent late subscribers from altering plugin hooks.
  • Search this index — Jump to any of 12 method pages above.

⚠️ Common Pitfalls

  • Anonymous handlers — Cannot remove what you did not store in a variable.
  • Duplicate add() — Without unique, the same fn runs twice on fire.
  • Confusing lock and disable — lock still fires; disable does not.
  • Wrong has()/fired() docs online — Both take no flags; has needs a function reference.
  • Removing after lock() — Silent no-op — check locked() first.

🧠 How Callbacks Works

1

$.Callbacks()

Factory creates an empty list, optionally with behavior flags.

Create
2

add(fn)

Handlers queue up in registration order.

Register
3

fire(data)

Every handler runs; flags and status methods apply.

Invoke
=

Organized callbacks

Modular handlers, clear lifecycle, testable event wiring.

Browser Support

jQuery Callbacks ships with jQuery 1.7+ and works wherever jQuery runs. All 12 methods in this hub follow the same API across jQuery 1.x, 2.x, and 3.x.

jQuery 1.7+

Callbacks API

Supported in all jQuery versions that include $.Callbacks(). Match your jQuery build to project requirements.

100% With jQuery
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 methods Universal

Bottom line: Safe for jQuery-based apps. Prefer jQuery 3.7+ for new projects alongside modern native Promise APIs.

🎉 Conclusion

jQuery Callbacks is a practical tool for managing groups of functions — from simple event buses to plugin hooks and Deferred internals. Start with $.Callbacks(), add(), and fire(), then explore flags and control methods as your needs grow.

Use the searchable index to open all 12 tutorials — each includes try-it labs and FAQs.

💡 Best Practices

✅ Do

  • Name handler functions for remove/has
  • Use once memory for init hooks
  • Expose subscribe APIs wrapping add/remove
  • Lock lists after registration closes
  • Disable lists on plugin destroy

❌ Don’t

  • Fire before handlers are registered (unless using memory)
  • Confuse Callbacks with Deferred
  • Expect remove() with no args to clear all
  • Add duplicate handlers without unique flag
  • Memorize all 12 names at once

Key Takeaways

Knowledge Unlocked

Five things to remember about Callbacks

Your gateway to 12 method tutorials.

5
Core concepts
+ 02

add / remove

Subscribe

List
03

fire

Invoke

Run
🔒 04

lock

Freeze

Control
12 05

Index

Search all

Ref

❓ Frequently Asked Questions

$.Callbacks() creates a managed list of functions. You add handlers with add(), invoke them with fire() or fireWith(), and optionally freeze or shut down the list with lock() or disable(). It powers custom event buses and jQuery Deferred internally.
Callbacks is a lower-level list manager — you control add, fire, and flags directly. Deferred builds on Callbacks to represent async task outcomes with resolve, reject, done, and fail. Use Callbacks for pub/sub; use Deferred for promises-style async.
once lets fire() run handlers only the first time. memory stores the last fire arguments and replays them to handlers added later. Combine as $.Callbacks("once memory") for init-style hooks.
lock() freezes the handler list — no more add/remove, but fire() still works. disable() permanently stops firing. lock protects subscribers; disable tears down the list.
fire(arg1, arg2) passes variadic arguments with default this. fireWith(context, [args]) sets this inside handlers and takes an argument array. Use fireWith when handlers need a specific object as context.
Read the overview, try the five examples, then open add() or jQuery.Callbacks() from Getting Started. Use the search box to jump to any of the 12 method tutorials.
Did you know?

jQuery Deferred’s done(), fail(), and always() queues are Callbacks lists under the hood. Learning Callbacks first makes Deferred internals much easier to understand.

Start with add()

Learn how to register handler functions on a Callbacks list.

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