setTimeout with or without anonymous function? What's the difference?
javascript, settimeout
Solution
You need to remove the parens in
st=setTimeout(checkme(),4000)
so instead:
st=setTimeout(checkme,4000)
otherwise, the function is invoked right away.
Since you have the same error inside the checkme function, it probably kills your browser due to unbounded recursion.
Problem
I used this code (followed by an xmlhttprequest that fills the "tcap" textarea): ``` st=setTimeout(checkme(),4000) ``` where `checkme()` is: ``` function checkme() { if (typeof (st) != 'undefined') clearTimeout(st) if (document.getElementById("tcap").innerHTML.length > 0) { document.getElementById('waitmsg').style.display = 'none' } else { st = setTimeout(checkme(), 1000) } } ``` If I run it, it freezes Firefox 19 with no error message. But if I replace the first argument (both in code and in the checkme() function) with: ``` st=setTimeout(function(){checkme()},4000) ``` it works correctly. So my question is: what's the difference in calling the `checkme()` function with or without the anon function? Why in the first case it freezes Firefox? Thanks