Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set a fixed time for javascript

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about how to set a fixed time in javascript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

The method of setting fixed time by javascript: 1. To execute js;2 after a fixed time period through setTimeout method, and to set fixed time execution by setInterval method.

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

Javascript set a fixed time?

JS timer use, fixed point, fixed time, cycle execution

Summary of this article: this paper mainly introduces the method of realizing fixed-point execution by JS and executing a function at a fixed time. For example, the method of execution at the next hour, at every hour, every 10 minutes.

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

Both of these methods can be used to implement JavaScript execution after a fixed period of time. In fact, setTimeout and setInterval have the same syntax. 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 which the code will be executed.

However, there is a difference between the two functions:

① setInterval () executes code or functions that are scheduled to be executed multiple times. After that fixed interval, it also repeats the code automatically.

② setTimeout () executes that code or specified function only once.

1. Circular execution

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

/ / cycle execution, every ten minutes. It will be executed for the first time in 10 minutes. SetInterval ("circulateExecute ();", 10: 60 / 1000); / / every 10 minutes

two。 Next time on the hour, or at some point in time.

The following javascript code implements the execution of the nextIntegralPointAfterLogin () method at the next point on the hour at the current time.

Var date = new Date (); / / now time var dateIntegralPoint = new Date (); / / the next hour when the user logs in can also be set to a fixed time dateIntegralPoint.setHours (date.getHours () + 1); / / increase the number of hours by 1dateIntegralPoint.setMinutes (0); dateIntegralPoint.setSeconds (0); setTimeout ("nextIntegralPointAfterLogin ();", dateIntegralPoint-date); / / execute at the next hour after the user logs in.

3. Execute at a fixed point on the hour.

After executing the nextIntegralPointAfterLogin () function at the next hour, as described above, in order to execute a function at every hour, you can write the following code in the nextIntegralPointAfterLogin () function.

Function nextIntegralPointAfterLogin () {IntegralPointExecute (); / / the function setInterval ("IntegralPointExecute ();", 60 hours 60 minutes 1000) is called at every hour; / / once an hour, then the next hour, the next hour will be executed}

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

These are all the contents of the article "how to set a fixed time for javascript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 255

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report