我试图在页面加载时调用我的类,并每隔 X 秒重新加载结果,但是在 setTimeout 教程之后,jquery 似乎给我抛出了一个错误,考虑到它是无语法的,我不明白。
未捕获的范围错误:超出最大调用堆栈大小
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);
添加括号时,您将直接调用该函数,而不会超时,并且在同一个函数内立即调用该函数很快就会变成一个无限循环,从而填满堆栈
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)