The difference between window.settimeout usage and window.setinterval usage

Source: Internet
Author: User

The function of SetTimeout and setinterval is a specified event or method that occurs after a certain time period.

The Window.settimeout method is a timed procedure, that is, what to do after a certain time,

Window.setinterval ()

Function: Invokes a function or evaluates an expression by the specified period (in milliseconds).

Syntax: SetInterval (CODE,MILLISEC)

Explanation: Code: The JavaScript code string to be executed at timed time.

MILLISEC: The set timing time, expressed in milliseconds.

Return value: The ID value of the timer, which can be used for the Clearinterval () method to stop the specified timer.

Note: the SetInterval () method keeps calling functions until the timer is terminated with clearinterval () or the window is closed.

Window.clearinterval ()

Function: Cancels the timer set by the SetInterval () method.

Syntax: Clearinterval (Id_of_setinterval)

Explanation: Id_of_setinterval: The ID value returned by setinterval (). This value identifies a setinterval timer.

That is, Window.setinterval () returns the Window.clearinterval parameter.

The difference between the two:

Window.settimeout ("function", time);//Set a timeout object, execute once, no cycle
Window.setinterval ("function", time);//Set a timeout object, period = ' interaction times '
Stop timing:
Window.cleartimeout (object) clears the SetTimeout object that has been set
Window.clearinterval (object) clears the SetInterval object that has been set

SetTimeout (expression, delay time);
SetInterval (expression, interaction time);
Delay Time/Interaction time is in Hao seconds (1000ms=1s)

The two expressions are very similar, but the function is very different, from the expression of the parameters can also be seen:
SetTimeout executes an expression once after it has been delayed for a specified time after loading and executes only once
SetInterval executes an expression once every specified time from loading, when it is executed

The difference between window.settimeout usage and window.setinterval usage

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.