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>