1

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:

  1. User sets interval time to 5000 ms
  2. SetInterval runs grading() every 5 sec
  3. -- User updated interval time to 10000 ms
  4. SetInterval updates the change and now runs grading() every 10 seconds
  5. .... and so on

Thanks!

noDe1
  • 331
  • 1
  • 5
  • 15
  • maybe you can use something like this.. https://shortify.tk/1kmx6r uses async await.. which is es2017 – cswl Dec 01 '16 at 14:31

1 Answers1

0

What you need is to be able to loop asynchronously to get the interval from the API while updating the interval. What you can do is use async.whilst and instead of doing SetInterval you can call setTimeout and just poll the API once the timeout is reached. In this example, you don't have to restart the program.

Try this out:

var CANCEL = false;
var async = require('async');

function grading(){
    console.log('in here');
}

async.whilst(function () {
  return CANCEL === false;
},
function (next) {
    defaultPoll.find(function (err, interval) {
        if (err) {
            return next(err);
        }
        setTimeout(function () {
            grading();
            next();
        }, interval);
    });
},
function (err) {
    // cancelled here
    console.error(err);
});
Corey Berigan
  • 614
  • 4
  • 14
  • hmm you code above does not seem to run grading at all. As in I run the code and it finishes without doing anything. Do I need to run a specific function? – noDe1 Nov 29 '16 at 18:42
  • This script works without `defaultPoll` as follows: `function (next) { setTimeout(function () { grading(); next(); }, 1000); }`. This tells me the loop works fine. There must be an error with your `defaultPoll` function. – Corey Berigan Nov 29 '16 at 19:10
  • I tried without the defaultPoll, however I need to restart the program for the effects to kick in. any idea why? – noDe1 Dec 02 '16 at 09:46
  • Anytime you modify any script you will always have to restart the program. – Corey Berigan Dec 02 '16 at 20:16
  • Your code still does not seem to work. I did a console.log(interval) value right after finding the value in the dbs, and it prints out the correct value. However your code just seems to run grading() non stop, without paying attention to the interval value - which in this case was 5000 milliseconds – noDe1 Dec 13 '16 at 11:31