Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

settimeout this. RangeError: Maximum call stack size exceeded

Tags:

javascript

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);
}
like image 659
muzaa18 Avatar asked Sep 05 '26 14:09

muzaa18


1 Answers

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>
like image 155
ThisClark Avatar answered Sep 08 '26 02:09

ThisClark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!