This is the simplest usage:
(Function (){
Alert ('One second later ...');
}). Delay (1000 );
This function will be automatically executed in 1 second.
Let's take a look at the implementation method of the delay function:
Delay: function (delay, bind, args ){
Return setTimeout (this. pass (args = null? []: Args), bind), delay );
}
Obviously, it is implemented using setTimeout, so we can clean it up. below is the implementation method:
Var Fun = function (msg ){
Alert ('fd ');
};
Var timer = Fun. delay (5000 );
// Clear timer after some operations
ClearTimeout (timer );
Next I will talk about his advanced usage:
Var Fun = function (msg ){
Alert (typeOf (this) // view the type of the bound object
Alert (this. get ('tag') + '|' + msg); // display the Object Name and transmitted Parameters
};
// Execute Fun in 2 seconds, and pass the id = 'tab1' object to the Fun function so that this is $ ('tab1'), and then pass a parameter
Fun. delay (2000, $ ('tab1'), 'Ah ');
The code in this section means that the latency is 2 seconds, and then the function Fun is executed, because I use this function, and by default, this corresponds to a window object, but I want this function to operate on a node. So I passed the binding to the node and then passed msg, in this case, when this is used in the function, the object is no longer a window, but the Node object that I bound to it.