Copy Code code as follows:
settimeout (function (obj) {
alert (OBJ.A);
}, Watts, {a:1});
The third argument is passed, and the third parameter is passed as the parameter obj of the callback function. 1 is popped up in non ie browsers. This has the advantage of resolving the execution context of the callback function, such as a method to invoke an object, which can be passed through parameters.
Copy Code code as follows:
settimeout (function (obj) {
Obj.method ();
}, Watts, obj);
Of course, you can also pass multiple arguments to the callback function, as follows
Copy Code code as follows:
settimeout (function (A, b) {
alert (a);
alert (b);
}, 2000, 1,2);
This time we passed two parameters 1,2 to the callback function, Firefox/safari/chrome/opera in turn, 1, 2. You can send more if you want.
Although IE does not support a third parameter, there is a difference between Firefox and Safari/chrome/opera.
Copy Code code as follows:
settimeout (function () {
alert (arguments.length);
}, 2000, 1,2);
Two arguments 1,2 to the callback function, and then alert to the length of the argument
Firefox:3
Safari/chrome/opera:2
Strangely, two parameters were passed, but Firefox was shot 3. If you output the third parameter, you will see that it is a number, sometimes a negative one.
Off
http://www.w3.org/TR/Window/
Https://developer.mozilla.org/en/DOM/window.setTimeout
Http://msdn.microsoft.com/en-us/library/ms536753%28v=vs.85%29.aspx
Solve the bug of settimeout transmission parameter under IE
Copy Code code as follows:
Solve the bug of settimeout transmission parameter under IE
if (!+[1,]) {
(function (Overridefun) {
Window.settimeout = Overridefun (window.settimeout);
Window.setinterval = Overridefun (window.setinterval);
})(
function (Originalfun) {
return function (code, delay) {
var args = [].slice.call (arguments, 2);
Return Originalfun (
function () {
if (typeof code = = ' String ') {
eval (code);
}
else {
Code.apply (this, args);
}
},
Delay
)
}
}
);
}