setTimeout和jQuery:未捕获RangeError:超出最大调用堆栈大小 [英] setTimeout and jQuery: Uncaught RangeError: Maximum call stack size exceeded

查看:273
本文介绍了setTimeout和jQuery:未捕获RangeError:超出最大调用堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在页面加载时调用我的类,以及在X秒内重新加载结果,但是在setTimeout教程之后,jquery似乎给我带来了一个我不明白的错误,因为它没有语法。 / b>


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



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

lhc();

函数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'){
返回true;
} else {
return false;
}
})。error(function(e){alert(error); console.log(e); });
if(rawlink.charAt(0)!='#if(href.match(^ javascript:)){
proceed = false;
}
'){
if(proceed){
$(this).after(< span style ='font-size:xx-small;'>(Hits:< span id =' + token +'>< / span>)< / span>);
$ .getJSON(lhc / link.php?link =+ link +& a = q,函数(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);
}


解决方案

更改

  setTimeout(lhc(),5000); 

  setTimeout(lhc,5000); 

当您添加括号并直接调用该函数时,您直接调用函数而不会超时离开相同的功能很快成为一个无尽的循环,填补了堆栈

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

这篇关于setTimeout和jQuery:未捕获RangeError:超出最大调用堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆