muzaa18
muzaa18

Reputation: 31

settimeout this. RangeError: Maximum call stack size exceeded

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

Upvotes: 3

Views: 2280

Answers (3)

Gabriel
Gabriel

Reputation: 2190

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

Upvotes: 0

ThisClark
ThisClark

Reputation: 14823

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>

Upvotes: 2

Alex Mac
Alex Mac

Reputation: 63

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.

Upvotes: 1

Related Questions