JavaScript Timer 1. What is a JavaScript timer?
In JavaScript, we can execute code after a set interval of time, not immediately after the function is called.
2. Timer type
One-time timer: Fires only once after a specified delay.
Interval Trigger timer: Fires once every interval of time
3.
Timer Method1): Disposable timer
A): SetTimeout (): Specify the delay after the execution of the code, go to execute once
Syntax: setTimeout (code, delay time);
Parameter description:
1. The function to invoke or the code string to execute.
2. Delay time: The amount of time, in milliseconds (1s=1000ms), to wait before executing the code.
B): Cleartimeout (): Cancel settimeout () setting
Syntax: cleartimeout (timer)
Parameter description:
Timer: The ID value returned by SetTimeout (). This value identifies the deferred execution code block to be canceled.
Call the settimeout () and cleartimeout () Delay methods:
<! DOCTYPE html>//Defining printing Methods functionPrint () {Console.log ("I'm printing!"); } varTimer//This value identifies the deferred execution code block to be canceled //Start printing functionStartprint () {Timer=settimeout (print,1000);//call timer, delay 1 seconds to print, execute only once } //End Printing functionStopprint () {cleartimeout (timer);//Cancel Timer } </script> </body>Call the settimeout () and cleartimeout () Infinite loop methods:
<! DOCTYPE html>
//Defining printing Methods functionPrint () {Console.log ("I'm printing!"); Timer=settimeout (print,1000);//Start the timer, call yourself, make an infinite loop } varTimer//This value represents a block of code to cancel deferred execution //Start printing functionStartprint () {Print ();//calling the Print method } //End Printing functionStopprint () {cleartimeout (timer);//Cancel Timer } </script> </body>2): Intermittent trigger TimerA): SetInterval (): Executes code at execution time, at specified intervals after loading the page
Syntax: setinterval (code, interaction time);
Parameter description:
1. Code: The function to invoke or the code string to execute.
2. Interaction time: The time interval between periodic execution or invocation of an expression, in milliseconds (1s=1000ms).
return value:
A value that can be passed to clearinterval () to suppress periodic execution of "code."
Call the function format ( assuming there is a clock () function ):
SetInterval ("clock ()", 1000) or setinterval (clock,1000)
B): Clearinterval () method to cancel the interaction time set by SetInterval ()
Syntax: clearinterval (timer)
Parameter description:
Timer: The ID value returned by SetInterval ().
Invoke SetInterval () and clearinterval () execution interval execution method instance
<! DOCTYPE html>//Defining printing Methods functionPrint () {Console.log ("I'm printing!"); } varTimer//This value identifies the timer execution code block to be canceled //Start printing functionStartprint () {Timer=setinterval ("Print ()", 1000);//Start Timer } //End Printing functionStopprint () {clearinterval (timer);//Cancel Timer } </script> </body>Talking about JavaScript timer