$ .Callbacks()。disable()vs $ .Callbacks()。lock() [英] $.Callbacks().disable() vs $.Callbacks().lock()

查看:105
本文介绍了$ .Callbacks()。disable()vs $ .Callbacks()。lock()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者的jQuery文档基本上都说明了同样的事情,所以我想知道两者之间是否存在任何重大差异(如果有的话)。谢谢!

The jQuery documentation for both basically states the same thing so I was wondering if there's any major difference, if any, between the two. Thanks!

推荐答案

关于此的文档实际上非常糟糕,所以这是我发现的研究源代码

The documentation on this is actually really bad, so here's what I found by studying the source code:

lock 仅阻止将来 fire 调用,但不会阻止函数 添加 ed。

以下是方法的快速概述:

Here's a quick rundown of the methods:


  • - 删除所有已注册的回调到目前为止。

  • lock - 阻止进一步调用 fire ,但允许更多回调添加 ed。

  • 禁用 - 阻止进一步调用 fire & 添加

  • empty - Removes any callbacks registered thus far.
  • lock - Prevents further calls to fire, but allows more callbacks to be added.
  • disable - Prevents further calls to both fire & add.

至了解这一切,让我们首先解释一下 内存 flag

To understand all this, let's start with an explanation of the memory flag:

如果使用 memory 标志构造回调对象,它将保持跟踪最后一次 fire 调用,稍后添加的任何回调将立即被调用。这是示例

If the callback object is constructed with the memory flag, it'll keep track of the last fire call, and any callback added later will immediately be called. Here's an example:

var callbacks = $.Callbacks('memory');

callbacks.add(function(){
    console.log('first');
});

callbacks.fire();

callbacks.add(function(){
    console.log('second');
});

这也会记录 second ,即使它是在 fire 之后添加的。

This'll also log second, even though it was added after the fire call.

如果你禁用但它会完全擦除内存。这是另一个例子

If you disable it though, it'll completely wipe the memory. Here's another example:

var callbacks = $.Callbacks('memory');

callbacks.add(function(){
    console.log('first');
});

callbacks.fire();
callbacks.disable();

callbacks.add(function(){
    console.log('second');
});

callbacks.fire();

这只会记录 第一个 ,因为回调在添加第二个功能之前已被禁用。

This'll only log first, since callbacks has been disabled before the second function was added.

但是,如果您使用 lock ,则稍后将添加添加的函数。这是另一个例子

However, if you use lock it instead, functions added later will be called. Here's another example:

var callbacks = $.Callbacks('memory');

callbacks.add(function(){
    console.log('first');
});

callbacks.fire();
callbacks.lock();

callbacks.add(function(){
    console.log('second');
});

callbacks.fire();

这也会记录 second ,但只有一次;由于对象是 lock ed,因此将忽略对 fire 的任何进一步调用。

This'll also log second, but only once; since the object was locked, any further calls to fire will be ignored.

这篇关于$ .Callbacks()。disable()vs $ .Callbacks()。lock()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆