Blue Syntax
Blue Syntax

Reputation: 33

How do i make my javascript code run every Sunday at 23:59 pm in my backend?

So I am making an app about weekly statistics and in my backend I want to make it reset all of my stats at Sunday 23:59 pm each week. How should i go about this so it does not disturb the rest of my backend, it is written in javascript and node (express)?

Upvotes: 1

Views: 763

Answers (3)

Josh Wulf
Josh Wulf

Reputation: 4877

You can use the cron package in Node to do it.

var CronJob = require('cron').CronJob;
var job = new CronJob('0 59 23 * * 0', function() {
  console.log('You will see this message every Sunday at 23:59');
}, null, true, 'America/Los_Angeles');
job.start();

Upvotes: 0

Monday A Victor
Monday A Victor

Reputation: 471

You can achieve that using a cron job. I will send you a link to an article hopefully that will help you complete your application.node cron jobs by example

#cheers

Upvotes: 0

eol
eol

Reputation: 24565

The node-cron library allows you to achieve this pretty easily:

const cron = require('node-cron');
const resetStats = () => { console.log("Resetting stats ...") };

// 59 23 * * SUN === every sunday at 23:59
cron.schedule('59 23 * * SUN', resetStats())

Upvotes: 3

Related Questions