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

javascript - setTimeout with or without anonymous function? What's the difference? - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Mar 12, 2013 at 8:43 dfsq 193k26 gold badges242 silver badges259 bronze badges asked Mar 12, 2013 at 8:37 Paolo AlfrediPaolo Alfredi 711 silver badge2 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 12

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.

setTimeout accepts a function as an argument, and the correct way to pass a function as an argument is either defining it as an anonymous function, or just providing the function name. If you use parenthesis(brackets), you aren't actually passing a function: You are executing the function and passing the result of the function to setTimeout.

Hence, when specifying a function in setTimeout, and anywhere else you need to pass a function as an argument, you should not use parenthesis.

You shouldn't be using the parenthesis within the setTimeout function. You should only be passing in a reference to the method. What you are doing is invoking the method and passing the return value in to the set timeout method.

If you are using setTimeout(checkme(),4000), you are passing the return value of checkme(); But if you want to pass it as a function you need to do in following ways

setTimeout(function(){checkme()},4000)

or

st=setTimeout(checkme,4000)
发布评论

评论列表(0)

  1. 暂无评论