Timer events in JS

Source: Internet
Author: User

Timer events in JS
Javascript can implement many functions that are not easy to complete in java code. Here we will learn some timer events in js. JavaScript executes code after a set interval, which is called a timing event. There are two methods to achieve this: 1. setInterval ()-the specified interval does not stop executing the specified code in milliseconds. 2. setTimeout ()-pause the specified code in milliseconds and execute the Specified Code. Both methods are window object methods. First, we will introduce the setInterval () method, which is worth executing the Specified Code continuously within milliseconds. Syntax: window. setInterval ("js Code, function, etc.", in milliseconds); instance 1: A hello setInterval (function () {alert ("Hello")}, 3000) pops up every three seconds ); instance 2: displays the current time. Use the button to stop the time and start <script type = "text/javascript"> var myVar; function startTimer () {/* setInterval () the specified code is executed continuously in milliseconds at the specified interval */myVar = setInterval (function () {myTimer ()}, 1000);} function myTimer () /* define a function to get the local time */{var d = new Date (); var t = d. toLocaleTimeString (); document. getElementById ("demo "). I NnerHTML = t;} function stopTimer () {/* clearInterval () method is used to stop the function code executed by the setInterval () method */clearInterval (myVar );} </script> setTimeout () method <body>

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.