Career development, code 100 days--2018-04-12
There are two types of timers in JavaScript, namely SetInterval and settimeout, which are used as follows:
Open:
SetTimeout ("function", time) sets a timeout object, delays execution, and executes only once
SetInterval ("function", time) sets a timeout object; continuous execution; repeated execution
* Both of the above methods have a return value, i.e. the timer object is changed. The object can be entered as a closed object.
Shut down:
Cleartimeout (object) clears the SetTimeout object that has been set
Clearinterval (object) clears the SetInterval object that has been set
such as the opening and closing of a Window object
var timer = window.setinterval (express,millseconds);
Window.clearinterval (timer);
var timer = window.settimeout (express,millseconds);
Window.cleartimeout (timer);
Timer Start-Stop use case:
Set two buttons to control the opening and closing of the timer, respectively
<type= "button" name= "Start" ID= "BTN1" Value= "Start"><type= "button" Name= "Stop" ID= "BTN2" value= "Stop">
JavaScript implements timer start-stop control
<script type= "Text/javascript" >//Timer Usage //var timer = window.setinterval (express,millseconds); Window.clearinterval (timer); //var timer = window.settimeout (express,millseconds); //window.cleartimeout (timer);Window.onload =function () { varBTN1 = document.getElementById ("BTN1"); varBTN2 = document.getElementById ("btn2"); varTimer =NULL; Btn1.onclick=function() {Timer=setinterval (function() {alert ("Hello"); },1000); }; Btn2.onclick=function() {clearinterval (timer); }; } </script>
Click "Start" button, every second pop-up box "Hello", that is, the timer turn on; Click "Stop" button, the prompt box no longer pops up, that is, the timer is off.
Day27-javascript Realization Timer and its application case