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

javascript - How would I get a function to run every 24 hours on a server? - Stack Overflow

programmeradmin6浏览0评论

I am using this code to make it run 24 hours after the program is executed but how do I make it run automatically on lets say heroku or something like this? How would I need to change the code?

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

Thanks!

Edit: I launched this exact code onto heroku as a web worker and it's been running once every 24 hours, just as I wanted. Nothing extra was needed.

I am using this code to make it run 24 hours after the program is executed but how do I make it run automatically on lets say heroku or something like this? How would I need to change the code?

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

Thanks!

Edit: I launched this exact code onto heroku as a web worker and it's been running once every 24 hours, just as I wanted. Nothing extra was needed.

Share Improve this question edited Aug 10, 2018 at 9:15 static_null asked Jul 12, 2018 at 8:57 static_nullstatic_null 3045 silver badges14 bronze badges 1
  • you need to create a page where you execute the function and put that page in cron and set the time every 24 hours. – PHP Web Commented Jul 12, 2018 at 8:59
Add a ment  | 

2 Answers 2

Reset to default 6

For this you can use node scheduler npm package https://www.npmjs./package/node-schedule

node scheduler allows you to schedule functions for execution at specific dates, with optional recurrence rules.

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

var j = schedule.scheduleJob('0 0 * * *', function(){
  console.log('The answer to life, the universe, and everything!');
});

Above code will execute a cron job when the minute is 0 and hour is 0. basically every day 0:0 in the mid night

use this site to generate formats for the scheduling

setInterval will only work if the script is kept running 24hrs a day.. otherwise you will need to set up a cron job to call the script for you this might be a good place to start: https://code.tutsplus./tutorials/scheduling-tasks-with-cron-jobs--net-8800

发布评论

评论列表(0)

  1. 暂无评论