SetTimeout() Method is used to call a function or computing expression after a specified number of milliseconds. The following describes how to use setTimeout () in the js class:
SetTimeout (expression, latency)
SetTimeout (expression, interaction time)
The delay time/interaction time is in the unit of luxury seconds (1000 ms = 1 s)
When setTimeout is executed, it is executed once after the specified delay after loading, and only once
When setTimeout is executed, it executes the expression once every specified time after loading.
1. Basic usage:
Run a piece of code:
- var i=0;
- setTimeout("i+=1;alert(i)",1000);
Execute a function:
- var i=0;
- setTimeout(function(){i+=1;alert(i);},1000);
Note: compare the two methods above.
Next, let's execute another function:
- var i=0;
- function test(){
- i+=1;
- alert(i);
- }
- setTimeout("test()",1000);
You can also do this:
- setTimeout(test,1000);
Summary:
The prototype of setTimeout is as follows:
- iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
SetTimeout has two forms:
- setTimeout(code,interval)
- setTimeout(func,interval,args)
Code is a string and func is a function. Note that the meaning of "function" is an expression rather than a statement. For example, if you want to periodically execute a function, you can write it as follows:
- function a()
- {
- //...
- }
- setTimeout("a()",1000)
Or
- setTimeout(a,1000)
Note that the second form is a. Do not write it as a (). Remember !!!
Expand, no matter what you write here, if it is a variable, it must be a variable pointing to a function; if it is a function, then its return value is a function.
2. use setTimeout to implement the setInterval function. The idea is very simple, that is, to call a function without stopping itself, a little like recursion.
- Var I = 0;
- Function xilou (){
- I + = 1;
- If (I> 10) {alert (I); return ;}
- SetTimeout ("xilou ()", 1000 );
- // You can also use this
- // SetTimeout (xilou, 1000 );
- }
3. use setTimeout in the class
Finally, the question is true. In fact, all the problems encountered in the class are about this. As long as this problem is solved, everything will be done. Haha. Let's analyze:
- Function xilou (){
- This. name = "xilou ";
- This. sex = "male ";
- This. num = 0;
- }
- Xilou. prototype. count = function (){
- This. num + = 1;
- Alert (this. num );
- If (this. num> 10) {return ;}
- // Perform the test in four ways, one by one.
- SetTimeout ("this. count ()", 1000); // A: an error occurs when calling x. count (): the object does not support this attribute or method.
- SetTimeout ("count ()", 1000); // B: Error display: an object is missing
- SetTimeout (count, 1000); // C: error message: 'Count' undefined
- // The fourth type is shown below.
- Var self = this;
- SetTimeout (function () {self. count () ;}, 1000); // D: Correct
- }
- Var x = new xilou ();
- X. count ();
Error analysis:
A: this actually refers to the window object, not the current instance object.
Count () and count in B: And C: actually refer to a separate function named count (), but it can also be window. count (), because window. count () can be omitted as count ()
D: Direct the variable self to the current instance object, so that the js parsing engine will not confuse who this refers.
Even though we know that setTimeout ("this. this in count () ", 1000) refers to the window object, but I still don't understand why it is the window object ^_^ (a little dizzy ...), then we can imagine how this setTimeout is defined: setTimeout is a method of window, the full name is like this: window. setTimeout (), which should be defined as follows:
- Window. setTimeout = function (vCode, iMilliSeconds [, sLanguage]) {
- // ...... Code
- Return timer // return a token
- }
So when this is passed in to setTimeout (), of course it refers to the current Object window to which it belongs.