-3

I am trying to create a loop in javascript where I want the function timeLoop() to be called for 10 seconds then stop. I am trying to do this with setInterval but it seems that it does not wait the timeout I set no matter how big I make the timeout time. Every time I run it I keep getting: RangeError: Maximum call stack size exceeded not sure how to create a timed loop that waits for some time before executing.

var seconds = new Date().getSeconds();

var mainFunc = function () {
        console.log("Time: " + seconds);
        timeLoop();
}


var timeLoop = function () {
        var newTime = new Date().getSeconds();
        var timer = newTime - seconds;
        console.log("New Time: " + newTime + " Elapsed time: " + timer);

        if(timer == 10) {
                clearInterval(timeLoop());
                return console.log("Times up 10 seconds!");
        }

        setInterval(mainFunc(), 10000);
}



mainFunc();
James111
  • 15,378
  • 15
  • 78
  • 121
Shawn Varughese
  • 450
  • 4
  • 17
  • 1
    Side note: [`clearInterval()`](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearInterval) expects an interval ID as its argument. `setInterval()` provides that to you as its return value. You'll have to store it, though. ([example](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval#Example_2_Alternating_two_colors)) – Jonathan Lonowski Sep 15 '16 at 02:40
  • @zerkms, this is not a duplicated question. It has a problem that looks like the other, but the cause is not the same. The issue in this code is about the wrong use of `clearInterval`. – Diego ZoracKy Sep 15 '16 at 02:44
  • @DiegoZoracKy it's both actually, and the problem with `clearInterval` is already clarified in the other comment. Please provide a better answer. – zerkms Sep 15 '16 at 02:47
  • 1
    That fixed it! Thanks so much! – Shawn Varughese Sep 15 '16 at 02:49
  • @DiegoZoracKy also see that OP could resolve their problem based on the duplicate + comment :-) – zerkms Sep 15 '16 at 02:50
  • @zerkms so, is solved! – Diego ZoracKy Sep 15 '16 at 02:55
  • @DiegoZoracKy what if you need to pass a variable to that function, how would you be able to do this then? – Shawn Varughese Sep 19 '16 at 15:28

1 Answers1

0

you must set interval only once, but in this code you set it each time method called. also if you want check time difference, get current time at start and then get time difference on timer.

change it and use setTimeout!

var startTime= new Date();
var mainFunc = function () {
    console.log("Time: " + startTime);
    timeLoop();
}

var timeLoop = function () {
    var newTime = new Date();
    // get time difference in miliseconds
    // if you want it in seconds easily: parseInt(timer/1000)
    var timer = newTime - startTime;
    console.log("New Time: " + newTime + " Elapsed time (miliseconds): " + timer);

    if(timer == 10000) {
            return console.log("Times up 10 seconds!");
    }

    setTimeout(mainFunc, 10000);
}
mainFunc();
SalmanAA
  • 552
  • 1
  • 6
  • 13
  • This is exactly how I want it thank you! The only thing now is that when comparing the time say if seconds get 59 and newTime get 00 its gets to a issue where its negative seconds and takes a lot longer to get to 10. Do you know they best way to implement this and fix this issue? – Shawn Varughese Sep 16 '16 at 04:51