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