最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ExpressJS: run a function every 24 hours - Stack Overflow

programmeradmin1浏览0评论

What's the easiest way to run an automated function every 24 hours in ExpressJS?

I have searched everywhere for a solution aside from running an infinite loop. Is this in principle the only way to do it?

What's the easiest way to run an automated function every 24 hours in ExpressJS?

I have searched everywhere for a solution aside from running an infinite loop. Is this in principle the only way to do it?

Share Improve this question asked May 22, 2020 at 15:24 Unconventional WisdomUnconventional Wisdom 3311 gold badge2 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

you need to use node-cron npm

var cron = require('node-cron');


cron.schedule('0 0 * * *', () => {
  console.log('running a task every day');
});

get other cron formula :https://crontab.guru/examples.html

In Javascript, you use setTimeout() and setInterval() to schedule something to run a specific amount of time into the future (details here). setTimeout() will run it once at the specific time interval from now. setInterval() will run it over and over again at that particular time interval.

If this is a server that's running constantly, you can just use setInterval().

setInterval(myFunction, 1000 * 60 * 60 * 24);

This will call your function myFunction every 24 hours.

There are also various chron packages such as node-schedule you can find on NPM that have more sophisticated scheduling tools (run it every other day and twice on Mondays) and can offer persistence so scheduling is remembered across a server restart.

For example, I have a home automation server running on a raspberry Pi that uses a setInterval() to run some log management code once a day.

发布评论

评论列表(0)

  1. 暂无评论