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();