The delay execution function _javascript skill of Javascript function object extension

Source: Internet
Author: User

Do you want to use the SetTimeout method of the Window object? Bingo, that's right! If you are in an application often to "delay the execution of a certain function", then based on the dry principle, can be extended for the function global object, add a delay to the functions such as delay, which will make your code more concise and effective.
The extension Function object adds delay method as follows:

Copy Code code as follows:

Function.prototype.delay=function (this1,timeout) {
this1=this1| | Null
timeout=timeout| | 0;
var _this=this;
var args=[];
Get parameters, note: The 1th and 2nd parameters are reserved parameters
Switch (arguments.length) {
Case 1:
Timeout=parseint (Arguments[0]);
Timeout=isnan (timeout) 0:timeout;
timeout=timeout<0?0:timeout;
Break
Default
for (Var i=0;i<arguments.length;i++) {
if (i>1) {Args.push (arguments[i]);
};
Break
};
var proxy=function () {
_this.apply (This1,args);
};
Return Window.settimeout (proxy,timeout);
};

Under the console of the Firefox firebug plug-in test, code like:
Copy Code code as follows:

var xx=function (n) {
This.name=n;
};
Xx.prototype.hi=function (a,b) {
Console.log (this.name+ "-" +a+ "-" +b);
};
var xx1=new xx ("Levin");
var t=xx1.hi.delay (xx1,1000, "Cocoa", "yoyo");
Xx1.hi ("Guluglu", "Jigujigu");

Related Article

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.