سؤال

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);
}
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You can to ignore error if running <script>window.onerror = function(){ return true;} </script> And set setTimeout(lhc, 5000); some.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top