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.