I have a node js program, which needs to run every 'x' amount of seconds. I have simplified my program, in order to see if there is a solution for my question.
This is my program:
defaultPoll.find(function (err, interval) {
if (err) return console.error(err);
console.log(interval);
})
setInterval(function(interval) {
grading();
}, interval, true);
function grading(){
console.log('in here')
}
The first thing that happens when I run this program, is that I find the interval value in the database (which the user has access through API), and the runs the setInterval
function, which prints out 'in here' every 5 seconds. So after 15 seconds my console looks like this:
in here
in here
in here
Now, the challenge - I would like the user to be able to modify the interval time - and have allowed an API for this. However, when the interval value is updated by the user, the program still runs using the old database value as if has not updated the setInterval method.
Is there a way to do this?
AIM:
- User sets interval time to 5000 ms
SetInterval
runsgrading()
every 5 sec- -- User updated interval time to 10000 ms
SetInterval
updates the change and now runsgrading()
every 10 seconds- .... and so on
Thanks!