Remove the function call and pull out the quotation marks. If you do not put quotation marks, setTimeout gets a direct link to a function that it can call later. However, if inside a line such as "checkForAllImagesLoaded" or "checkForAllImagesLoaded()" , then it will execute the passed code when the timeout occurs.
At this time, the global object (window) will search for checkForAllImagesLoaded , but it is not defined there, so the reason you get the undefined error.
Your code is wrapped in an anonymous self-starting function, and checkForAllImagesLoaded does not exist outside it. Therefore, pass a direct reference to the function in the setTimeout call, not the string.
setTimeout(checkForAllImagesLoaded, 20);
setTimeout can be called using a function (and optional arguments) or a string containing JavaScript code:
var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]); var timeoutID = window.setTimeout(code, delay);
Anurag
source share