最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - too much recursion when call function(params) with timeout - Stack Overflow

programmeradmin7浏览0评论

i'm having a problem when recursion function.i'm get the error in firebug

too much recursion

this is my javascript code:

var contentPc = "list";
waitForBody(contentPc);
function waitForBody(id){
    var ele = document.getElementById(id);
    if(!ele){
        window.setTimeout(waitForBody(contentPc), 100);
    }
    else{
        //something function
    }
}

how i can fix this? thanks for your answer.

i'm having a problem when recursion function.i'm get the error in firebug

too much recursion

this is my javascript code:

var contentPc = "list";
waitForBody(contentPc);
function waitForBody(id){
    var ele = document.getElementById(id);
    if(!ele){
        window.setTimeout(waitForBody(contentPc), 100);
    }
    else{
        //something function
    }
}

how i can fix this? thanks for your answer.

Share Improve this question asked Nov 23, 2011 at 7:06 viyancsviyancs 2,3394 gold badges40 silver badges72 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Presumably, you don't have an id="list" element in your DOM. That would mean that your initial waitForBody call would end up here:

window.setTimeout(waitForBody(contentPc), 100);

and that will call waitForBody(contentPc) while building the argument list for setTimeout. And then you end up back at the setTimeout call again but one more stack level deep. I think you mean to say this:

window.setTimeout(function() { waitForBody(contentPc) }, 100);

so that the next waitForBody call is delayed a little bit.

发布评论

评论列表(0)

  1. 暂无评论