Deep understanding of settimeout function and setinterval function _javascript techniques

Source: Internet
Author: User
Tags setinterval time interval

A few days ago to learn JS, see two very interesting functions, they are settimeout functions and setinterval functions, these two functions can make the Web page rendering very some of the more common Web page effect, such as Picture Carousel, and some very fun effect. Let's take a look at these two functions!

Syntax and application of a settimeout function and setinterval function

1.setTimeout function

Definition and Usage: the settimeout () method is used to call a function or evaluate an expression after a specified number of milliseconds.

Grammar: settimeout (CODE,MILLISEC);

Parameters:

Code (required): The JavaScript code string to execute after the function to be invoked.

Millisec (required): The number of milliseconds to wait before executing code.

Tips:

SetTimeout () executes only one code at a time. If you want to call more than once, use SetInterval () or let code itself call SetTimeout () again.

return value

A value that can be passed to Window.cleartimeout () to cancel the periodic execution of code.

Since settimeout is a timer function, then there is a function to clean up the timer, then we use the Cleartimeout function.

Cleartimeout (settimeout () the ID value returned);

2.setInterval definition

The SetInterval () method can call a function or a calculation expression in the specified period (in milliseconds).

The SetInterval () method keeps calling the function until the clearinterval () is called or the window is closed. The ID value returned by SetInterval () can be used as an argument to the Clearinterval () method.

Grammar

SetInterval (code,millisec[, "Lang"]);

Parameter code required. The function to invoke or the code string to execute.

Millisec must. The time interval between the periodic execution or invocation of code, in milliseconds

return value

A value that can be passed to Window.clearinterval () to cancel the periodic execution of code.

Since settimeout is a timer function, then there is a function to clean up the timer, so we use the clearinterval () function.

Clearinterval () (SetInterval () the ID value returned);

Two. Case:

Countdown effect

<! DOCTYPE html>  

Background picture Toggle:

<! DOCTYPE html>
 
 

Roll-Call Device

<! DOCTYPE html>  

Through a few cases above, I believe that we should not be unfamiliar with these two functions.

The above in-depth understanding of the settimeout function and setinterval function is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.