. (New users may think that setTimeout and setinterval are JavaScript Functions, which is incorrect. Beginners can easily confuse JavaScript Object functions with DOM object methods .)
FirstCodeLet's guess what the results will look like in various browsers?Copy codeThe Code is as follows: function f (){
VaR S = 'arguments. Length: '+ arguments. Length + ';';
For (VAR I = 0, n = arguments. length; I <n; I ++ ){
S + = '[' + I + ']:' + arguments [I] + ';';
}
Alert (s );
}
SetTimeout (F, 500, "JavaScript", "AAA ")
What I want to discuss here is not when to use it, but the difference between the two methods in different browsers.
I have never thought that these two methods would have had any problem. I was notified by an occasional opportunity. Now I want to write it out and share it with you.
Because the parameters and usage of setTimeout and setinterval are the same, but the functions are different, in order to save trouble, I will only use setTimeout as an example to describe and give an example.
The most frequently used form of setTimeout is probably the following two types:Copy codeThe Code is as follows: itimerid = setTimeout (strjscode, 50) // strjscode is a string containing JS Code
Itimerid = setTimeout (objfunction, 50) // The objfunction is a function object.
The first method of calling is to upload a string containing the JS Code. The advantage of this method is conciseness, but the disadvantage is that the running efficiency is poor, and it is not conducive to Syntax Parsing and has potential risks. More importantly, it is difficult to deal with complicated content, which is consistent with the disadvantages of eval.
Therefore, we think it is better to use the second method. (The following examples all use 2nd call methods)
Now let's reveal the results of the first piece of code in various browsers:
IE (6, 7, 8) is: arguments. Length: 0;
Opera (6, 7, 8) is: arguments. Length: 2; [0]: javascript; [1]: AAA;
Firefox (3.0) is: arguments. Length: 3; [0]: javascript; [1]: AAA; [2]:-15;
There is such a big difference, it is really "you sing your song, I am not my tune "!
In Firefox (3.0), the last number is not specific. Sometimes it is 0 or a negative number.
(1) setTimeout In the IE Series
First, let's look at how Microsoft's DHTML reference manual says:
SetTimeout Method
Evaluates an expression after a specified number of milliseconds has elapsed.
Syntax
Itimerid = Window. setTimeout (vcode, imilliseconds [, slanguage])
Parameters
Vcode required. variant that specifies the function pointer or string that indicates
The code to be executed when the specified interval has elapsed.
Imilliseconds required. integer that specifies the number of milliseconds.
Slanguage optional. String that specifies one of the following values:
JScript Language is JScript.
VBScript language is VBScript.
Javascript language is JavaScript.
Description of setTimeout on msdn:
The http://msdn.microsoft.com/en-us/library/ms536753 (vs.85). aspx
That is to say, setTimeout receives three parameters, and 3rd parameters indicate the type of the script language. If you input more parameters, it is meaningless.
Therefore, in IE, either of the following is true.
SetTimeout ('alert (1) ', 50 );
SetTimeout ('msgbox "ends, retries, and ignores the request. ", Vbabortretryignore + vbdefaultbutton2," tell you "', 50, 'vbscript ');
(2) setTimeout in Mozilla Series
Let's take a look at what gecko DOM reference says on Mozilla's official website:
Window. setTimeout
Summary
Executes a code snippet or a function after specified delay.
Syntax
VaR timeoutid = Window. setTimeout (func, delay, [param1, param2,...]);
VaR timeoutid = Window. setTimeout (Code, delay );
The first two parameters are the same and there is no difference. The third parameter is different.
Currently, only the IE browser supports scripts in multiple languages. other browsers only support JS scripts, so language parameters are not required.
Mozilla uses the first parameter that is passed to setTimeout and the more parameters that follow it to the preceding func as the parameter.
Firefox, opera, Safari, chrome, and so on.
However, Mozilla mentioned that the setTimeout of his family has a bug called "lateness" argument.
"Lateness" argument
Functions invoked by setTimeout are passed an extra "lateness" argument in Mozilla,
I. e., the lateness of the timeout in milliseconds. (See Bug 10637 and bug 394769 .)
In the example at the beginning, Firefox (3.0) has the root of an Oolong number.
Description of setTimeout on Mozilla:
Https://developer.mozilla.org/en/DOM/window.setTimeout
(3) setTimeout in other browser series (opera, Safari, Chrome)
It is basically the same as that in the Mozilla series, but there is no bug with one more parameter in the Mozilla series.
Wu Lin: Tips for using setTimeout
(1) pass parameters to the call function in setTimeout in IE
According to the above analysis, ie does not support passing parameters to the called function in setTimeout. To ensure the harmony of the browser world, we can wrap the function call parameters into the new anonymous function. Example:Copy codeThe Code is as follows: function f (){
Alert ();
}
// SetTimeout (F, 50, 'Hello'); // used for non-ie
SetTimeout (function () {f ('hello')}, 50); // common
VaR STR = 'hello ';
SetTimeout (function () {f (STR)}, 50); // common
(2) This problem
The context when the setTimeout function is called is global, instead of the context when the setTimeout method is called. Therefore, when the function of the first setTimeout parameter is executed, this points to window. If you need to retain this when the setTimeout method is called, you need to pass this in. Example:Copy codeThe Code is as follows: function person (name ){
This. Name = Name;
VaR F = function () {alert ('My name is '+ this. Name )};
// SetTimeout (F, 50); // Error
VaR this = this;
SetTimeout (function () {f. Apply (this)}, 50); // correct, common
SetTimeout (function () {f. Call (this)}, 50); // correct, common
}
New person ('jack ');
that's all you have to say.
posting is not a mental task, but a physical task, text organization, example writing, and typographical task. The least technical task is the most time-consuming task.