Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

When to use flags

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "when to use flags". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "when to use flags".

Possible flags: (available flags:)

Once: make sure the callback list only executes (.fire ()) once (like a deferred Deferred).

Memory: keep the previous value and immediately invoke any callback (like a deferred Deferred) with the latest value added to the list.

Unique: make sure you can only add one callback at a time (so there are no duplicate callbacks in the list).

StopOnFalse: interrupts the call when a callback returns false

By default, the callback list can be triggered multiple times as in the callback list of events.

For an example of how flags should be used ideally, see below:

$.Callboxes ("once"):

Var callbacks = $.Callboxes ("once")

Callbacks.add (fn1)

Callbacks.fire ("foo")

Callbacks.add (fn2)

Callbacks.fire ("bar")

Callbacks.remove (fn2)

Callbacks.fire ("foobar")

/ *

Output:

Foo

* /

$.Callboxes ("memory"):

Var callbacks = $.Callboxes ("memory")

Callbacks.add (fn1)

Callbacks.fire ("foo")

Callbacks.add (fn2)

Callbacks.fire ("bar")

Callbacks.remove (fn2)

Callbacks.fire ("foobar")

/ *

Output:

Foo

Fn2 says:foo

Bar

Fn2 says:bar

Foobar

* /

$.Callboxes ("unique"):

Var callbacks = $.Callboxes ("unique")

Callbacks.add (fn1)

Callbacks.fire ("foo")

Callbacks.add (fn1); / / repeat addition

Callbacks.add (fn2)

Callbacks.fire ("bar")

Callbacks.remove (fn2)

Callbacks.fire ("foobar")

/ *

Output:

Foo

Bar

Fn2 says:bar

Foobar

* /

$.Callboxes ("stopOnFalse"):

Function fn1 (value) {

Console.log (value)

Return false

}

Function fn2 (value) {

Fn1 ("fn2 says:" + value)

Return false

}

Var callbacks = $.Callboxes ("stopOnFalse")

Callbacks.add (fn1)

Callbacks.fire ("foo")

Callbacks.add (fn2)

Callbacks.fire ("bar")

Callbacks.remove (fn2)

Callbacks.fire ("foobar")

/ *

Output:

Foo

Bar

Foobar

* /

Thank you for your reading, the above is the content of "when to use flags", after the study of this article, I believe you have a deeper understanding of when to use flags, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report