Usage of setTimeout () in JS

Source: Internet
Author: User

How to use SetTimeout () in JS class
SetTimeout (expression, delay time)
SetTimeout (expression, interaction time)
Delay Time/Interaction time is in Hao seconds (1000ms=1s)

SetTimeout executes an expression once after it has been delayed for a specified time after loading and executes only once
SetTimeout executes an expression once every specified time from loading, when it is executed

1, Basic usage:
Execute a section 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 the differences between the two methods above are compared.

Here's another one to execute the function:
var i=0;
function Test () {
I+=1;
alert (i);
}
SetTimeout ("Test ()", 1000);
You can also do this:
SetTimeout (test,1000);

Summarize:
The prototype of settimeout is this:
Itimerid = Window.settimeout (Vcode, Imilliseconds [, Slanguage])

There are two forms of settimeout

SetTimeout (Code,interval)
SetTimeout (Func,interval,args)

Where code is a string
Func is a function.

Note that the meaning of "function" is an expression, not a statement.
For example, you want to perform a function periodically
function A () {
//...
}
can be written as
SetTimeout ("A ()", 1000)
Or
SetTimeout (a,1000)

Note here that the second form is a, do not write a (), remember!!!
Unfold, 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 will be a function

2, using settimeout to realize the function of setinterval
The idea is simply to call in a function to execute itself, a bit like recursion
var i=0;
function Xilou () {
I+=1;
if (i>10) {alert (i); return;}
SetTimeout ("Xilou ()", 1000);
You can use this, too.
SetTimeout (xilou,1000);
}

3, using settimeout in the class
Finally to the point, in fact, in the use of the class you encounter problems are about this, as long as the solution of this problem on everything without worry.
Oh. Let's take a look at the analysis:

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;}
The following tests are in four ways, one in turn.
SetTimeout ("This.count ()"),//a: An error occurs when the following X.count () Call: The object does not support this property or method.
SetTimeout ("Count ()",//b): Error display: Missing object
SetTimeout (count,1000);//c: Error display: ' Count ' undefined
Here is the fourth type
var self=this;
SetTimeout (function () {self.count ();},1000);//d: Correct

}

var x=new xilou ();
X.count ();

Error Analysis:
A: This in fact refers to the Window object, not the current instance object
B: and C: Count () and count actually refer to a single function named count (), but can also be window.count (), because Window.count () can be omitted as count ()
D: The variable self points to the current instance object, so that the JS parsing engine does not confuse the "who" this refers to.

Anyway, although we know that this in settimeout ("This.count ()", 1000) refers to the Window object, it still does not understand why it is
Window Object ^_^ (a little dizzy ...)
So we can imagine how this settimeout is defined:
SetTimeout is a method of window, the full name is this: Window.settimeout ()
That's supposed to be defined like this:
Window.settimeout=function (Vcode, Imilliseconds [, Slanguage]) {
//..... Code
Return timer//Returns a marker
}
So when you pass this to settimeout (), it is, of course, the current object window to which it belongs.

Usage of setTimeout () in JS

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.