How to use setTimeout () and setInterval () in js classes

Source: Internet
Author: User
SetTimeout (expression, delay time); Unit: ms (ms); 1s1000ms; setInterval (expression, interaction time); Unit: ms (ms); 1s1000ms; window. when setTimeout () is executed, it executes an expression or function at the specified time after loading; only

SetTimeout (expression, delay time); Unit: ms (ms); 1 s = 1000 ms;

SetInterval (expression, interaction time); Unit: ms (ms); 1 s = 1000 ms;

Window. setTimeout ()

During execution, it executes an expression or function from the specified time delay after loading; only once; used with window. clearTimeout.

Window. setInterval ()

During execution, it executes an expression or function at a specified time after loading the page; (the function is similar to a recursive function); it is used with window. clearInterval.

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

// 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)

The code is a string.

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

Function (){
//...
}

Writable

SetInterval ("a ()", 1000)

Or

SetInterval (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 simple, that is, calling a function without stopping the execution of itself is a bit 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 (){
// By the West Building cold month www.chinacms.org
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 display: count undefined
// The fourth type is "by the West Building cold month" www.chinacms.org.
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 this in setTimeout ("this. count ()", 1000) refers to the window object, we still don't understand why

Window object ^_^ (a little dizzy ...)

Then we can imagine how this setTimeout is defined:

SetTimeout is a window method. The full name is as follows: window. setTimeout ()

It 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.

 

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.