Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setTimeout(job.stop, 5000) does not stop the job #65

Closed
elgs opened this issue Oct 31, 2013 · 2 comments
Closed

setTimeout(job.stop, 5000) does not stop the job #65

elgs opened this issue Oct 31, 2013 · 2 comments

Comments

@elgs
Copy link

elgs commented Oct 31, 2013

I believe this is not a problem of node-cron, but rather a question in javascript. The following code demonstrates the question. The cron job starts without any problem. However, it doesn't stop with the code setTimeout(job.stop, 5000), whereas it stops as expect with the following code:

// Stops as expected
setTimeout(function () {
    job.stop();
}, 5000);
var cronJob = require('cron').CronJob;
var job = new cronJob('* * * * * *', function () {
    console.log('You will see this message every second');
});

job.start();

// Does not stop
// setTimeout(job.stop, 5000); 

// Stops as expected
setTimeout(function () {
    job.stop();
}, 5000);
@ncb000gt
Copy link
Member

ncb000gt commented Nov 1, 2013

This is due to scoping. The stop function uses this to identify the timeout to stop and to fire the onComplete callback. When you just provide the function pointer, the scoping isn't for the job, it is for the timeout which doesn't have the objects expected in this function (namely _timeout and onComplete).

@ncb000gt ncb000gt closed this as completed Nov 1, 2013
@elgs
Copy link
Author

elgs commented Nov 7, 2013

Thanks @ncb000gt, that answers my question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants