Talking about JavaScript timer

Source: Internet
Author: User
Tags setinterval

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 Timer

A): 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

Related Article

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.