Reputation: 31
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
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
Reputation: 14823
Several things...
function
keyword to define your startTime
function.this
keyword in the setTimeout
reference to startTime
. 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
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