Home  >  Article  >  Web Front-end  >  How to set a fixed time in javascript

How to set a fixed time in javascript

藏色散人
藏色散人Original
2021-06-18 11:31:143681browse

How to set a fixed time in javascript: 1. Use the setTimeout method to execute js after a fixed period of time; 2. Use the setInterval method to set a fixed time for execution.

How to set a fixed time in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript sets a fixed time?

Use of JS timer, fixed time, fixed time, loop execution

Overview of this article: This article mainly introduces the implementation of timed and fixed point execution through JS. A method to execute a function at a fixed time. For example, the method is to execute on the next hour, execute on every hour, and execute regularly every 10 minutes.

There are two timer methods in JavaScript: setTimeout() and setInterval().

Both methods can be used to execute JavaScript after a fixed period of time. In fact, the syntax of setTimeout and setInterval are the same. They all have two parameters, one is the code string to be executed, or the function name, and the other is the time interval in milliseconds. After that time period, the code will be executed.

However, there are differences between these two functions:

① setInterval() will execute the code or function to be executed regularly multiple times. After that fixed time interval, it will automatically repeat the execution of the code.

② setTimeout() will only execute that piece of code or the specified function once.

1. Loop execution

The following JS statement implements the circulateExecute() method every ten minutes.

//循环执行,每十分钟一次。10分钟后第一次执行。
       setInterval("circulateExecute();",10*60*1000);//10分钟执行一次

2. Execute at the next hour, or at a fixed point at a certain time

The following javascript code implements the nextIntegralPointAfterLogin() method at the next hour at the current moment.

 var date = new Date();//现在时刻
var dateIntegralPoint = new Date();//用户登录时刻的下一个整点,也可以设置成某一个固定时刻
dateIntegralPoint.setHours(date.getHours()+1);//小时数增加1
dateIntegralPoint.setMinutes(0);
dateIntegralPoint.setSeconds(0);
setTimeout("nextIntegralPointAfterLogin();",dateIntegralPoint-date);//用户登录后的下一个整点执行。

3. Fixed-point execution at every hour

After executing the nextIntegralPointAfterLogin() function at the next hour introduced above, in order to execute a certain function at every hour, you can Write the following code in the nextIntegralPointAfterLogin() function.

function nextIntegralPointAfterLogin(){
         IntegralPointExecute();//在整点执行的函数,在每个整点都调用该函数
setInterval("IntegralPointExecute();",60*60*1000);//一个小时执行一次,那么下一个整点,下下一个整点都会执行
}

Note: Due to the error in JS calculation and the time required for execution, the above timed and fixed-point execution method may have an error of one or two seconds.

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to set a fixed time in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn