0

Im trying to make the cron function run every randomInterval number of seconds.

Every time I run this it says Maximum call stack size exceeded

--UPDATE: I changed setInterval(cron(), randomInterval); to setInterval(cron, randomInterval); which removed the maximum call stack error. But this will still not act as a good reliable cron. After the first 5 iterations it will execute the cron function with out setting an interval.

Any help would be appreciated!!!

function cron() {
    randomInterval = randomIntFromInterval(1000, 9000);

    cronInterval = setInterval(cron, randomInterval);
}

function randomIntFromInterval(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

cron();

The code below has the tests logging out the intervals and also when the cron fires.

function cron() {
    var d = new Date();

    var h = d.getHours();
    var m = d.getMinutes();

    h = (h < 10) ? "0" + h : h;
    m = (m < 10) ? "0" + m : m;

    console.log(h + ":" + m + "took screenshot ");

    randomInterval = randomIntFromInterval(1000, 9000);

    cronInterval = setInterval(cron, randomInterval);
    console.log(randomInterval/1000);
}

function randomIntFromInterval(min, max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

cron()
isethi
  • 557
  • 1
  • 6
  • 16

0 Answers0