IE does not support the third or above parameter of settimeout/setinterval function _javascript technique

Source: Internet
Author: User
Tags setinterval
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
)
}
}
);
}

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.