Too much recursion when the call function (params) timed out

I have a problem with a recursive function. I get an 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 can i fix this? thanks for your reply.

+4
source share
1 answer

Presumably you do not have an id="list" element in your DOM. This would mean that your initial waitForBody call ends here:

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

and this will call waitForBody(contentPc) when building the argument list for setTimeout . And then you return to the setTimeout call again, but one more level level. I think you want to say this:

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

so the next call to waitForBody little delayed.

+9
source

Source: https://habr.com/ru/post/1382646/


All Articles