SetInterval () and setTimeout () usage and difference example _ basic knowledge

Source: Internet
Author: User
SetInterval () and setTimeout () must be familiar to everyone. Anyone who is familiar with js knows this, but some new users are not very familiar with the usage of the two, the following is a brief introduction. 1. setInterval () usage _ Learning

The Code is as follows:

// Method for Automatic execution every second
Var c = 0;
Function showLogin ()
Alert (c ++ );
// SetInterval method or string, millisecond, parameter array (method ))
SetInterval ("showLogin ()", "1000 ");

2. setTimeout

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 haoss (1000 ms = 1 s). When setTimeout is executed, it is executed once after the specified delay time after loading, only once setTimeout is executed. During execution, 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 );

// Compare the two methods above.
Next, let's execute another function:

The Code is as follows:

Var I = 0;
Function test (){
I + = 1;
Alert (I );
SetTimeout ("test ()", 1000 );

You can also do this:
SetTimeout (test, 1000 );
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 (){
SetTimeout ("a ()", 1000)
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 (automatically execute the function at intervals)
The idea is simple, that is, calling a function without stopping the execution of itself is a bit like recursion.

The Code is as follows:

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:

The Code is as follows:

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

Simple Example:

The Code is as follows:

My Heading 1

Click here!

Var I = 1;

Function clickButton (){
Document. getElementById ("click"). click ();
I ++;

SetInterval ("clickButton ()", "1000 ");
// SetTimeout ("clickButton ()", 1000 );
// SetTimeout (clickButton, 1000 );
Function change (){
If (I % 2 = 1)
Document. getElementById ('id1'). style. color = 'red ';
Document. getElementById ('id1'). style. color = 'black ';



Related Article

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