JS in the custom timer

Source: Internet
Author: User

Sometimes, in the cause of demand, we need to write a method in JS, and then let it execute at a certain moment, that is, the need to write a timer in JS, when the time to meet the required time, the method needs to be executed automatically, the following small part of a simple how I realized (reprint please specify the reprint address: blog.csdn.net/u012116457)

           var tminutes=0;           var thours=0;           var go;           function Dingshi (hours,minutes) {              thours = hours;              Tminutes = minutes;              Go=setinterval (run,3000);           }           function run () {                   var date=new date ();                   if ((Date.getminutes ()-tminutes==0)                           && (date.gethours ()-thours==0)) {                 clearinterval (go);                                    GetData ();   The method to execute                }                      }           }
The parameter Hours,minutes in Dingshi is the time to start the method to be executed, which only requires hours and minutes, in which case you can add parameters yourself, but be careful to modify the condition in the IF in the Run method.

GetData is to execute the method, but also according to the actual situation of self-modification, when using the Dingshi method can be called.

Also note that in order to prevent browser crashes,the second parameter of SetInterval I set to 3000 milliseconds, that is, 3 seconds, if you have time to request accurate to the second, here should be changed to 1000, otherwise you may miss the set of times.

JS in the custom timer

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.