简体   繁体   中英

How do I run Node.js api after certain interval of time?

I want to send notification to my android and ios app by using REST API of Node.js?

I know it can be done via chrone method but I don't know how to set up that system so I can send push notification to my app users when ever I want (after certain interval of time).

You can use setInterval method in pure JavaScript

setInterval(function () {
    //Call to method which will send notification
    // If you have specific endpoint then call to that endpoint
}, 3000);

For cron job there is also many npm packages are available.

/* You can use node-cron module. The node-cron module is tiny task scheduler in pure JavaScript for node.This module allows you to schedule task in node. */

var cron = require('node-cron');
cron.schedule('1,2,4,5 * * * *', function() {
  console.log('running every minute 1, 2, 4 and 5);
});

/* Above example will run the callback function every 1,2,4,5 minute. You can also provide range such as 1-3 for every 3 min duration.enter code here */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM