This article describes: This paper mainly introduces the implementation of timed fixed-point through JS, at a fixed time to perform a function of the method. For example, execute at the next hour, on every hour, and every 10 minutes at timed intervals.
There are two timer methods in javascript: SetTimeout () and SetInterval ().
Both of these methods can be used to implement JavaScript after a fixed time period. In fact, settimeout and setinterval have the same syntax. They all have two parameters, one is the code string that will be executed, or the function name, and one is the time interval in milliseconds, and the code will be executed after that time period.
However, there are differences between the two functions:
①setinterval () Executes the code or function that is scheduled to execute more than once. After that fixed time interval, it also automatically repeats the code.
②settimeout () executes only one piece of code or the specified function.
1. Circular implementation
The following JS statement implements the Circulateexecute () method every 10 minutes.
Loop execution, once every 10 minutes. 10 minutes after the first execution.
setinterval ("Circulateexecute ();", 10*60*1000);//10 Minute execution
2. Next hour, or at some point, execute.
The following JavaScript code implements the implementation of the Nextintegralpointafterlogin () method at the next hour point fixed-point at the current moment.
var date = new Date ();/now time
var dateintegralpoint = new Date ();//The next hour of the user's logon hour can also be set to a certain fixed time
Dateintegralpoint.sethours (Date.gethours () +1);//hours increased by 1
dateintegralpoint.setminutes (0);
Dateintegralpoint.setseconds (0);
SetTimeout ("Nextintegralpointafterlogin ();", dateintegralpoint-date);//The next hour after the user logs on.
3. Every point of the hour to execute
After executing the Nextintegralpointafterlogin () function in the next hour described above, you can write the following code in the Nextintegralpointafterlogin () function in order to implement a function on every hour.
function Nextintegralpointafterlogin () {
integralpointexecute ()//functions executed on the whole hour, which are called on each of the hour
setinterval (" Integralpointexecute (); ", 60*60*1000);//one-hour execution time, then the next whole point, the next hour will be executed.
Note: Due to the error of JS calculation and the implementation of the process requires a certain amount of time, so the timing of the above fixed-point execution method may have a two-second error.
The above JS timer use, timing fixed-point, fixed moment, circular implementation of the detailed is small set to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.