Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

too much recursion when call function(params) with timeout

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.

like image 206
viyancs Avatar asked Dec 28 '22 10:12

viyancs


1 Answers

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.

like image 95
mu is too short Avatar answered Dec 31 '22 15:12

mu is too short