jquery provides developers with external interface calls, but $. The callbacks () module is developed to give internal $.ajax () and $. The Deferred () module provides a unified basic functional component. It can be used as a function of a new component defined in a similar basis.
jquery.callbacks is jQuery in the 1.7 version after the addition, is from the 1.6 version of the _deferred object, mainly used for function queue Add, remove, fire, lock and other operations, and provide once , memory, unique, stoponfalse four option for some special control.
The common scenario for this function is the event triggering mechanism, which is the publication and subscription mechanism of the observer pattern in the design pattern, and the current callbacks object is used in queue, Ajax, and deferred objects.
Ajax Modules
- Ajax:function (URL, options) {
- Omitting code
- deferred = Jquery.deferred (),
- completedeferred = Jquery.callbacks ("Once Memory")
- ..............
- }
It's not hard to find that Jquery.callbacks also provides parameters such as "Once Memory" for processing:
☑once: Make sure that this callback list executes only (. Fire ()) once (like a deferred Deferred).
☑memory: Keep the previous value and immediately execute the call to any callback (like a deferred Deferred) with the most recent 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.
- var callbacks = $. Callbacks (' once ');
- Callbacks.add (function () {
- Alert (' a ');
- })
- Callbacks.add (function () {
- Alert (' B ');
- })
- Callbacks.fire (); Output result: ' A ' ' B '
- Callbacks.fire (); Not executed
The once function is to make the callback queue run only once.