Champa
Champa

Reputation: 615

Create a scheduled function in node

How would one go about creating a function to fire up automatically every 24 hours ?

I have created a typing game for kids and I would like to see how much traffic comes at the end of each day.

I have the function ready, but I cant seem to figure out how to schedule it.

I have tried to use cron, but with no success.

Upvotes: 1

Views: 352

Answers (2)

Senthil
Senthil

Reputation: 2246

As @blue112 mentioned you can use node-cron or node-schedule etc . Here is an example how you can have web application cum cron service running on the node js application. You not required to use linux cron to trigger those details.

Ref : https://scotch.io/tutorials/nodejs-cron-jobs-by-examples

Upvotes: 0

blue112
blue112

Reputation: 56582

There are many ways to achieve that.

If you application is running non-stop and you don't plan to stop it, you can use setInterval to run a function every 60 seconds. Here, you can check if it's "00:00" for instance (using the native Date class) and run your function then.

Another way is to use the node-cron library, which is designed for that use.

Upvotes: 2

Related Questions