Cancel the node-scheduling of the event after setting it

I plan to use node-schedule to configure push notifications for meeting reminders as follows:

var schedule = require('node-schedule');
var date = req.body.date;   // Date for reminder from client request 
var j = schedule.scheduleJob(date, function(){
    // Send push notification
});

Now I know that you can cancel the event by calling cancel ():

j.cancel()

However, how can I refer to this specific cancellation task if the client-side user decides to change the appointment to a different date?

Thanks in advance for your help!

+4
source share
2 answers

To get a specific task, you can create it with a unique name, and you can get it later through this unique name:

var j = schedule.scheduleJob(unique_name, date, function() {
});
// later on
var my_job = schedule.scheduledJobs[unique_name];
my_job.cancel();
+11
source
var randf = require('randomstring');    
    var jobId = randf.generate(10); //randomsrting
    var at = new Date();
    var time = at.setSeconds(at.getSeconds() + Number(5))  //after 5 second     
    schedule.scheduleJob(jobId, new Date(time),function(){
         schedule.cancelJob(jobId);
         console.log('hi--viral');
     });
0
source

Source: https://habr.com/ru/post/1611508/


All Articles