Day27-javascript Realization Timer and its application case

Source: Internet
Author: User
Tags setinterval

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

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.