JQuery Callbacks Application key points

Source: Internet
Author: User

Callbacks is a method introduced by jquery 1.7 to manage a series of callback functions that use the same parameters. All callback functions (hereinafter referred to as callbacks) are stored in an array and can be called repeatedly. It is essentially equivalent to a callback function column (list), so you can add, delete, empty callback functions and so on.

Generate callback Column (callbacks)

var callbacks = $. Callbacks ();

Callback Execution order

The callback is saved in the array and then traversed through a for loop, so the callbacks in the column are executed sequentially in the order in which they were added, and the last addition is generally last executed.

Fire the items on the Listvar foo = function (value) {  console.log ("foo:" + value);};/ /Add Another function to the Listvar bar = function (value) {  console.log ("bar:" + value);}; var callbacks = $. Callbacks (); Callbacks.add (foo); Callbacks.add (bar); Callbacks.fire ("Hello"); output:foo:hello//Output:bar:hello

The only exception is if the token is memory and if the fire () was called before, then the new callback is added () and will be executed once with the previous fire's parameters. But the callback that was called by the Fire () before add (), if not using fire () or Firewith (), will not be executed again immediately.

Four marks: Once, memory, unique, Stoponfalse

    • Once: Each callback in the column executes at most once, and after execution completes, the callback function column is emptied.
    • Memory: Remember the parameters of the previous fire (), once the new callback is added, it is executed once with the last parameter (the previously added callback does not execute).
    • Unique: The same callback cannot be added repeatedly.
    • Stoponfalse: If a callback returns false, subsequent callbacks are no longer executed.

Example of how to set a tag:

var callbacks = $. Callbacks ("Once Memory");

After calling Callbacks.disable () on other keys, callbacks can no longer be enabled.

After calling disable (), the callback function column is emptied, and there is no response to using fire or firewith. Therefore, the callbacks does not provide the Enable method because all callbacks have been emptied and no more necessary to enable.

Call Callbacks's Fire () or Firewith () in the callback function

One scenario is if you call Callbacks's Fire () or Firewith () in a callback, what should I do? jquery does this by using fire () or Firewith () in the callback, callbacks simply saves the parameters of Fire () or Firewith () and does not immediately execute the callback in the column. The new parameters will be exploited by callbacks in the callbacks only after all callbacks in callbacks have been executed.

function Fn1 (value) {   console.log (value);    if (value = = "bar!") return false;    Callbacks.fire ("bar!");} function Fn2 (value) {  console.log ("fn2 says:" + value);} var callbacks =$. Callbacks ("Stoponfalse"); Callbacks.add (FN1); Callbacks.add (FN2); outputs:foo!//outputs:fn2 says:foo!//outputs:bar!callbacks.fire ("foo!");

Callbacks.lock ()

The callback column is locked and the call to Callbacks.fire () or Callbacks.firewith () is invalidated.

If you call Callbacks.lock () in a callback, there is one thing to note:

    • Callbacks has memory Tags: callbacks that are not executed in the current fire () or Firewith () methods continue to execute, but Callbacks.fire () and Callbacks.firewith () in the callback will no longer work.
    • Callbacks no memory tag: All callbacks are emptied, which means that subsequent callbacks are no longer executed.

Strangely, callbacks does not provide the Unlock method, which means that once locked,callbacks is permanently lost the ability to call fire () or Firewith ().

JQuery Callbacks Application key points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.