The callback object callbacks analysis of jquery

Source: Internet
Author: User

Callbacks's position in jquery is the foundation of deferred, and of course, its methods are disclosed externally.

The callbacks object is a powerful tool to manage multiple functions uniformly. The core is the arbitrary combination of several status codes in the parameters options, which is very flexible.

Let's take a look at options

Once: Triggers only one callback

Memory: If the post-trigger state is triggered immediately after the add

Unique: Adds the same return function, triggering only one of the

Stoponfalse: Return False is encountered in the callback and is not executed later.

The first is:

list = []

Our job is to maintain this list.

Addfunction() {                if(list) {//First , we save the current length                    varStart =list.length; (functionAdd (args) {Jquery.each (args,function(_, arg) {varType =Jquery.type (ARG); if(Type = = = "function" ) {                                if(!options.unique | |!Self.has (ARG))                                {List.push (ARG); }                            } Else if(Arg && arg.length && type!== "string" ) {                                //Inspect recursivelyAdd (ARG);                    }                        });                    }) (arguments); //Do we need to add the callbacks to the                    //Current firing batch?                    if(firing) {Firinglength=list.length; //with memory, if we ' re not firing then                    //we should call right away}Else if(memory) {Firingstart=start;                    Fire (memory); }                }                return  This; },

At its core is the push method. Why is the code so much? Support array mode parameters, encountered in the parameters of memory situation, the method of execution.

Remove method:

Removefunction() {                if(list) {Jquery.each (arguments,function(_, arg) {varindex;  while(index = Jquery.inarray (ARG, list, index)) >-1) {list.splice (index,1 ); //Handle Firing Indexes                            if(firing) {if(Index <=firinglength) {Firinglength--; }                                if(Index <=Firingindex) {Firingindex--;                }                            }                        }                    }); }                return  This; },

Its core is also the splice method.

For this firing, the following is introduced.

Fire Method:

Fire =function(data) {memory= Options.memory &&data; Fired=true; Firingindex= Firingstart | | 0; Firingstart= 0; Firinglength=list.length; Firing=true;  for(; list && Firingindex < firinglength; firingindex++ ) {                if(list[firingindex].apply (data[0], data[1]) = = =false&&options.stoponfalse) {memory=false;//To prevent further calls using Add                     Break; }} Firing=false; if(list) {if(Stack) {if(stack.length) {Fire (Stack.shift ()); }                } Else if(memory) {list= []; } Else{self.disable (); }            }        }

Its core is which loop, take it out one after the other to execute.

There's a stack here.

stack =!options.once && []

This is an array, why do you use it? This is for some functions that are time-delay functions, in which case the fire may be delayed and put into the stack function first.

In this way, the above firing state can be understood.

This is the callbacks overall, its core principle is relatively simple.

The callback object callbacks analysis of jquery

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.