3
votes

Why RangeError: Maximum call stack size exceeded in this.startTime();

startTime() {
    $('.m-codeModal-resendTimer').html(this.sectmr);
    this.sectmr--;
    if (this.sectmr < 0) {
        this.sectmr = 0;
        $('.m-codeModal-resendErrorNow').fadeIn(0);
        $('.m-codeModal-resendErrorSec').fadeOut(0);
        $('.m-codeModal-sorry').fadeOut(0);
    }
    setTimeout(this.startTime(), 1000);
}
3
You probably want to pass a function to the timer, not to instantly call it. - Роман Парадеев

3 Answers

2
votes

Several things...

  • Add a function keyword to define your startTime function.
  • Remove the this keyword in the setTimeout reference to startTime.
  • The function setTimeout takes a callback as a parameter. You, instead of passing a callback parameter to the function, are actually calling the startTime function before the setTimeout function ever has a chance to evaluate and count down 1000 milliseconds.

Here's a simplified example:

var count = 0;
function startTime() {
    count++;
    document.getElementById('count').innerHTML = count;
    setTimeout(startTime, 1000);
}
startTime();
<div id="count"></div>
1
votes

You're in an infinite loop.

by calling startTime() function call for the first time, you are recursively calling startTime again once you enter the setTimeout function.

In your startTime() function as it is now, there is no way to exit it once you enter.

Maybe you'd want to try

if (this.sectmr < 0) {
  ...
  return;
}

by adding the return statement, once your sectmr goes below zero and enters the if loop, you should be kicked out of the function. I'm not sure what your end goal is, however. Please be a bit more descriptive in the opening question.

0
votes

The problem is that you're doing setTimeout(this.startTime(), 1000);, executing this.startTime() and using its return value (undefined in this case) as the timer handler. Just remove the ().