This article introduces how to use callback objects in jquery. I hope this tutorial will be helpful to you.
The Callback function is executed after the current animation 100% is complete.
JQuery animation Problems
Many jQuery functions involve animation. These functions may use speed or duration as optional parameters.
Example:
The Code is as follows: |
Copy code |
$ ("P"). hide ("slow ") |
The speed or duration parameter can be set with many different values, such as "slow", "fast", "normal", or millisecond.
Instance
The Code is as follows: |
Copy code |
$ ("Button"). click (function (){ $ ("P"). hide (1000 ); }); |
Try it yourself
Because JavaScript statements (commands) are executed one by one in order, the animated statements may produce errors or page conflicts because the animation is not completed yet.
To avoid this problem, you can add the Callback function as a parameter.
The Code is as follows: |
Copy code |
Var foo = function (value ){ Console. log (this ); Console. log ('foo: '+ value ); }; Var bar = function (value ){ Console. log ('bar: '+ value ); }; Var callbacks = $. Callbacks (); // Add the foo Method to the callbacks queue Callbacks. add (foo ); // Add the bar method to the callbacks queue Callbacks. add (bar ); // Cancel callback // Callbacks. disable (); // Delete All callback Methods // Callbacks. empty (); // Return the parameter list to the callback list Callbacks. fire ('foo '); // Use callbacks. fired () to confirm. If the callback in the list is called at least once, true is returned; otherwise, false is returned. // Console. log (callbacks. fired ()); Var myObj = { }; /* Access all callbacks in the given context and parameter list Context: context Reference of the callback triggered in the list Args: a parameter or parameter list is returned to the callback list */ Callbacks. fireWith (myObj, ['foo']); /** * Check whether the callback function exists in the callback list. */ Console. log (callbacks. has (foo )); // The list of callbacks that are locked in the current status. // Callbacks. lock (); // The callback object is invalid after the lock. // Callbacks. fireWith (myObj, ['foo']); // Check whether the callback list is locked. True is returned for locking. false is returned for non-locking. // Console. log (callbacks. locked ()); // Delete the set of callback or callback lists. Callbacks. remove (foo) Callbacks. fireWith (myObj, ['foo']); |