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");