Customize the timer in JS to make it execute _ javascript skills at a certain time

Source: Internet
Author: User
Write a method to execute it at a certain time. That is, you need to write a timer in JS. When the time reaches the required time, the method to be executed is automatically executed, for the following example, you can refer to the following: Sometimes, for reasons that are in demand, we need to write a method in JS and then let it execute at a certain time, that is to say, you need to write a timer in JS. When the time reaches the required time, the method to be executed is automatically executed. The following section briefly describes how I implemented the timer.

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 (); // method to be executed }}}

In the parameter hours in dingshi, minutes is the start time of the method to be executed. here only the hour and minute are required. You can add the parameter by yourself under specific circumstances, however, be sure to modify the if judgment condition in the run method.

GetData is the method to be executed. It is also modified according to the actual situation. You can call the dingshi method when using it.

It should also be noted that in order to prevent the browser from crashing, the second parameter of setInterval is set to 3000 milliseconds, that is, 3 seconds. If you require precision to seconds, you should change it to 1000 here, otherwise, you may miss the set time.

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.