Advanced usage of delay in Mootools

Source: Internet
Author: User

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.

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.