Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout and jQuery: Uncaught RangeError: Maximum call stack size exceeded [duplicate]

I'm trying to call my class as the page loads, as well as well as reload the results ever X seconds, however following setTimeout tutorials, jquery seems to tossing me a error i don't understand considering it's syntaxlessness.

Uncaught RangeError: Maximum call stack size exceeded

var rand = function() {
    return Math.random().toString(36).substr(2);
};

lhc();

function lhc(){
    $('#lhcb a').each(function() {
        var rawlink = $(this).attr("href");
        var link = encodeURIComponent( rawlink );
        var token = rand();
        var href = $(this).prop('href');
        var proceed = $.getJSON( "lhc/link.php?link=" + link + "&a=c", function( data ) {
            if ( data.proceed == 'true' ) {
                return true;
            } else {
                return false;
            }
        }).error(function(e) { alert("error"); console.log(e);});
        if ( href.match("^javascript:") ) {
            proceed = false;
        }
        if ( rawlink.charAt(0) != '#' ) {
            if ( proceed ) {
                $(this).after( " <span style='font-size:xx-small;'>( Hits: <span id='" + token + "'></span> )</span>" );
                    $.getJSON( "lhc/link.php?link=" + link + "&a=q", function( data ) {
                        $('#' + token).html(data['hits']);
                    }).error(function(e) { alert("error"); console.log(e);});
                $(this).attr( "href", "lhc/link.php?link=" + link + "&a=g" );
            }
        }

    });
    setTimeout(lhc(), 5000);
}
like image 843
WASasquatch Avatar asked Mar 29 '14 18:03

WASasquatch


People also ask

How do you fix uncaught RangeError maximum call stack size exceeded?

The call stack is limited in size, and when it's exceeded, the RangeError is thrown. This can happen when a deeply nested function is called, or when a lot of new variables are created. The most common way to fix this error is to reduce the number of function calls, or to limit the number of variables that are created.

How do I stop max call stack size exceeded?

How to Avoid RangeError: Maximum Call Stack Size Exceeded. If this error is encountered when calling recursive functions, it should be ensured that the function has a defined base case to terminate the recursive calls.

What does it mean when maximum call stack size exceeded?

The JavaScript exception "too much recursion" or "Maximum call stack size exceeded" occurs when there are too many function calls, or a function is missing a base case.

What is the maximum call stack size JavaScript?

The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified.


1 Answers

Change

setTimeout(lhc(), 5000);

to

setTimeout(lhc, 5000);

You're calling the function directly without a timeout when you add the parenthesis, and calling the function right away inside the same function quickly becomes an endless loop that fills up the stack

like image 65
adeneo Avatar answered Oct 13 '22 22:10

adeneo