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);
}
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 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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With