setTimeout和jQuery:Uncaught RangeError:超出最大调用堆栈大小

WAS*_*tch 10 javascript jquery function settimeout

我正在尝试在页面加载时调用我的类,以及重新加载结果X秒,但是在setTimeout教程之后,jquery似乎折腾了一个错误,我不明白它是无语法.

未捕获RangeError:超出最大调用堆栈大小

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);
}
Run Code Online (Sandbox Code Playgroud)

ade*_*neo 26

更改

setTimeout(lhc(), 5000);
Run Code Online (Sandbox Code Playgroud)

setTimeout(lhc, 5000);
Run Code Online (Sandbox Code Playgroud)

添加括号时,您可以在没有超时的情况下直接调用函数,并且在同一函数内立即调用函数会很快成为填充堆栈的无限循环