jQuery callbacks.lock() Method — Freeze List State

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

What Is lock()?

callbacks.lock() locks a Callbacks list in its current state (since jQuery 1.7). After that, further fire() / fireWith() calls do not invoke handlers with new values. Returns the same Callbacks object for chaining.

Remember
callbacks.add(foo)
callbacks.fire("hello")   // runs
callbacks.lock()
callbacks.fire("world")   // ignored — list is locked

locked() → true after lock()
NeedUse
Freeze current statecbs.lock()
Is it locked?cbs.locked()
Full shutdowncbs.disable()
Late subscribers + last value$.Callbacks("memory") then lock()

How to Use lock()

Fire (or set memory), then lock when the state should stop changing.

  1. Create a list — use "memory" if late adds should still see the last fire.
  2. add() handlers and fire() as needed.
  3. Call lock() when values / state are final.
  4. Check with locked(); expect later fire() to be ignored.
PieceRequired?Role
Arguments—None
Return—Same Callbacks object (chainable)
Status—locked() becomes true

Pseudocode

Pseudocode
function lock(callbacks):
    mark callbacks as locked
    // without memory: further useful firing stops
    // with memory: late add() may still run with last args
    return callbacks

Cheat Sheet

Freeze state — pair with locked() to read status.

TopicDetail
Syntaxcallbacks.lock()
SincejQuery 1.7
After lockNew fire() values ignored
With memoryLate add() can still run last args
Status checklocked()
Cousindisable()

Live Preview

Fire, lock, then try again — watch handlers and locked().

Browser demo of callbacks.lock() — same idea as the examples below.

Result
Ready — pick an action and click Run.

jQuery Examples

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

Example 1 — Lock Stops Later fire()

Official style: fire once, lock, fire again — second call is ignored.

jQuery
var foo = function (value) {
  console.log("foo:" + value);
};

var callbacks = $.Callbacks();
callbacks.add(foo);
callbacks.fire("hello");
// foo:hello

callbacks.lock();
callbacks.fire("world");
// (no output)
Try It Yourself

How It Works

  1. Add foo and fire("hello") — handler runs.
  2. lock() freezes the list in that state.
  3. fire("world") does not call foo.
  4. Takeaway. New fire values are ignored after lock.

Example 2 — Check With locked()

Status flips to true after lock().

jQuery
var cbs = $.Callbacks();
cbs.add(function (value) {
  console.log("foo: " + value);
});

cbs.fire("hello");
console.log(cbs.locked());  // false

cbs.lock();
console.log(cbs.locked());  // true
Try It Yourself

How It Works

  1. Before lock. locked() is false.
  2. Call lock(). State freezes.
  3. After. locked() returns true.
  4. Use. Guard code that must not re-publish.

Example 3 — memory + Late add()

After lock, new fire values stay ignored; late adds still get the last args.

jQuery
var lines = [];
var foo = function (value) {
  lines.push("foo: " + value);
};
var bar = function (value) {
  lines.push("bar: " + value);
};

var cbs = $.Callbacks("memory");
cbs.add(foo);
cbs.fire("hello");
cbs.lock();

cbs.fire("world");              // ignored
cbs.add(bar);
cbs.fire("silentArgument");     // late add runs with "hello"
console.log(lines.join("\n"));
// foo: hello
// bar: hello
Try It Yourself

How It Works

  1. memory stores the last fire arguments.
  2. lock blocks new fire values ("world" ignored).
  3. Late add(bar) plus another fire still uses memorized "hello".
  4. Official rule. Memory lists can add after lock.

Example 4 — lock() vs disable()

Both stop new fires; disable() is the harder shutdown.

jQuery
function demo(label, action) {
  var lines = [];
  var cbs = $.Callbacks("memory");
  cbs.add(function (v) { lines.push(v); });
  cbs.fire("first");
  action(cbs);
  cbs.fire("second");
  cbs.add(function (v) { lines.push("late:" + v); });
  cbs.fire("ignored");
  console.log(label + " → " + lines.join(", "));
}

demo("lock", function (cbs) { cbs.lock(); });
// lock → first, late:first

demo("disable", function (cbs) { cbs.disable(); });
// disable → first
Try It Yourself

How It Works

  1. Both ignore fire("second").
  2. lock + memory still delivers memorized value to a late add.
  3. disable stops further useful work — late add does not run.
  4. Choose. Final values + late subscribers → lock+memory; full teardown → disable.

Edge Cases & Pitfalls

Most confusion is assuming fire() still publishes after lock().

fire

New fires are ignored

After lock(), fire("world") does not run handlers — that is the official default behavior.

memory

Without memory, late adds help less

The special “add after lock” path is for lists created with $.Callbacks("memory").

unlock

No unlock API

There is no unlock(). Need a fresh publish cycle? Create a new $.Callbacks() list.

status

Use locked(), not guesswork

locked() is the boolean check. Do not confuse it with disabled().

Browser Support

callbacks.lock() has shipped since jQuery 1.7 with $.Callbacks. It works in all modern browsers with jQuery loaded.

✓ jQuery 1.7+

callbacks.lock()

Freeze a Callbacks list so later fire() calls with new values are ignored.

100% Browser support
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.lock() Excellent

Bottom line: Call lock() when the list state is final. Pair with memory if late subscribers should still see the last value.

Performance Notes

ApproachCostBest for
lock()O(1) flagFreeze publish state
fire() after lockEarly exitSafe no-op
memory + late addO(1) per addOne-shot results + late joiners

Locking is cheap. Prefer clear lifecycle (fire → lock) over ad-hoc flags outside Callbacks when this model fits.

Key Takeaways

  • Role: freeze the list in its current state.
  • Fires: later fire() values are ignored.
  • memory: late add() can still get last args.
  • Status: read with locked().

One line: lock() freezes state — new fires stop; with memory, late adds can still see the last value.

Frequently Asked Questions

lock() freezes a Callbacks list in its current state. After lock(), further fire() / fireWith() calls with new arguments do not run handlers. It returns the same Callbacks object for chaining.
Both stop new fires from invoking handlers. With the memory flag, lock() still lets you add late handlers that receive the last memorized fire values. disable() shuts the list down more completely — “do anything more” is off.
No. Call lock() with no arguments. Flags such as memory belong on $.Callbacks("memory") when you create the list.
Officially: if the list was created with memory, additional functions may still be added after lock() and will run with the memorized last arguments — even though new fire values are ignored.
Call locked() — it returns true after lock(). That is the status check; lock() is the action.
When the publish values are final (especially with memory for late subscribers), or you want to freeze further state changes after a one-shot workflow without treating it exactly like disable().

Next: locked()

Check whether a Callbacks list has been locked.

locked() 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.

8 people found this page helpful