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
Background picture Toggle:
Roll-Call Device
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.