jQuery callbacks.lock() Method — Freeze List State

Beginner
5 min read
Updated: Nov 2026
3 examples
Callbacks

Definition

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.

Syntax

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

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

locked() → true after lock()
CallWhat it does
ArgumentsNone
ReturnSame Callbacks object (chainable)
Statuslocked() becomes true

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

Parameters

ParameterDescription
Step 1Create a list — use "memory" if late adds should still see the last fire.
Step 2add() handlers and fire() as needed.
Step 3Call lock() when values / state are final.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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.

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.

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

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: callbacks.locked() Method — Check Lock Status

Check whether a Callbacks list has been locked.

callbacks.locked() Method — Check Lock Status 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.

8 people found this page helpful