2014-03-29 36 views
10

我試圖調用我的類作爲頁面加載,以及重新加載結果有史以來X秒,但setTimeout教程,但jquery似乎拋出我考慮到它是無語法的,我不明白這個錯誤。setTimeout和jQuery:未捕獲RangeError:超過最大調用堆棧大小

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); 
} 
+2

這不是重複的。這裏的問題是對'lhc()'的無效調用,這實際上是一個簡單的語法錯誤。它導致相同的消息,但起源是非常具體的。 – Dariusz

回答

25

變化

setTimeout(lhc(), 5000); 

setTimeout(lhc, 5000); 

你直接調用該函數沒有超時時加括號,並調用函數內馬上相同的功能很快就會變成一個無盡的循環,填滿了堆棧

+0

謝謝,這很有道理。現在我只是要弄清楚我的令牌問題,並重用令牌來更新,而不是它下降一個全新的 lol oops – WASasquatch

+0

@ adeneo感謝和+1。你救了我:)但一個問題,你的意思是我不能發送參數的功能,當我想爲它設置超時。現在和使用這種方法,我將參數更改爲全局變量,但它不是那麼可愛和好。你現在可以如何設置一個函數的超時時間,並在其中發送參數。 – QMaster

+0

@QMaster closure - 'setTimeout(function(){lhc(param1,param2)},5000)' – daviestar

0

你可以忽略錯誤,如果運行 <script>window.onerror = function(){ return true;} </script> 並設置setTimeout(lhc, 5000);一些。

+1

設置窗口。 onerror'在你的腳本之前。 –

相關問題