如何在每小时的确切分钟执行函数?

huangapple go评论59阅读模式
英文:

React: how can I execute function at exact minute every hour?

问题

You can execute the function at the exact minute (1 and 31 minutes, for example) every hour by using a cron expression. In Chinese, it would be:

你可以使用一个cron表达式来在每个小时的准确分钟(例如1和31分钟)执行函数。

英文:

Is there any way I can execute the function at the exact minute (1 and 31 minutes for example ) every hour?
For example, it should be executed at:

08:01
08:31
09:01
09:31
10:01
...

答案1

得分: 3

你可以看一下Croner,它可以评估和运行Cron表达式。

这个包还可以返回Cron表达式的下N个运行时间列表。

Crontab.guru也很方便解析这些表达式。

对于你的示例,Cron表达式将是1,31 * * * *,意味着它会在每分钟和每小时的31分钟运行。

const pattern = '1,31 * * * *';

const job = Cron(pattern, () => {
  console.log('Job is running...');
});

const nextTimes = Cron(pattern).nextRuns(10);
console.log('Next run times:', nextTimes.map(d => d.toLocaleTimeString()));
<script src="https://cdn.jsdelivr.net/npm/croner@6/dist/croner.umd.min.js"></script>
.as-console-wrapper { max-height: 100% !important; }
英文:

You could have a look at something like Croner, this can evaluate and run Cron expressions.

This package can also return a list of the next N runtimes for a cron expression.

Crontab.guru is handy for parsing these expressions too.

For your example, the cron expression will look like `1,31 * * * *', meaning it will run at every minute and at thirty-one minutes past every hour.

<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->

const pattern = &#39;1,31 * * * *&#39;

const job = Cron(pattern, () =&gt; {
  console.log(&#39;Job is running...&#39;);
});

const nextTimes = Cron(pattern).nextRuns(10);
console.log(&#39;Next run times:&#39;, nextTimes.map(d =&gt; d.toLocaleTimeString()));

<!-- language: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/croner@6/dist/croner.umd.min.js"></script>
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; }
<!-- end snippet -->

答案2

得分: 3

You can use a cron 来执行此操作,或者使用 setInterval 从头开始创建一个计时器,该计时器将每 n 毫秒执行一次回调函数(在您的情况下,每 60000 毫秒执行一次)。

然后,在回调函数中,您只需检查 new Date().getMinutes() 的值(1 或 31),并在条件满足时执行该函数。

如果要在整点时调用该函数,您可以从每秒执行回调函数的 setInterval 开始,直到下一分钟,然后使用上述方法。

例如:

function forEveryMinutes() {
  setInterval(() => {
    const currentMinutes = new Date().getMinutes();
    if([1, 31].includes(currentMinutes)) {
      // ...
    }
  });
};

const secondsInterval = setInterval(() => {
  const currentSeconds = new Date().getSeconds();
  if(!currentSeconds) {
    clearInterval(secondsInterval);
    forEveryMinutes();
  }
}, 1000);
英文:

You can use a cron to do that, or do it from scratch with a setInterval to create a timer that will execute a callback every n millisecond (in your case every 60000).

Then in the callback you will only have to check the value of new Date().getMinutes() (1 or 31) and execute the function if the condition pass

setInterval(() =&gt; {
  const currentMinutes = new Date().getMinutes();
  if([1, 31].includes(currentSeconds) {
    // ...
  }
}, 60000);

If you want the function to be called at exactly 0 second, you can start with a setInterval executing the callback every second until the next minute, then use the above method

For example:

function forEveryMinutes() {
  setInterval(() =&gt; {
    const currentMinutes = new Date().getMinutes();
    if([1, 31].includes(currentSeconds) {
      // ...
    }
  });
};

const secondsInterval = setInterval(() =&gt; {
  const currentSeconds = new Date().getSeconds();
  if(!currentSeconds) {
    clearInterval(secondsInterval);
    forEveryMinutes();
  }
}, 1000);

答案3

得分: 0

基本上,如果你使用 setInterval 或任何其他 JavaScript 包,要记住它只会在页面打开时运行。否则它将不会工作。

所以,假设在你的应用程序中有一个订阅系统。现在无论用户是否打开了应用程序,你都必须在特定时间段后取消订阅。所以,在这些情况下,你不能使用 JavaScript。

现在对于这些情况,你需要使用 corn job。你还可以使用 数据库触发器

英文:

Basically if you use setInterval or any other javascript package, keep it in mind that it will work only when the page is opened. Otherwise it will not work.

So, let say in your application, you have subscription system. Now whether user has opened the application or not, you have to expired subscription after specific time period. So, for these cases, you can't use javascript.

Now for these cases, you need corn job. You can also use Database Triggers.

huangapple
  • 本文由 发表于 2023年5月10日 19:28:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定