小编Pao*_*edi的帖子

有或没有匿名函数的setTimeout?有什么不同?

我使用了这段代码(后跟一个填充"tcap"textarea的xmlhttprequest):

st=setTimeout(checkme(),4000)
Run Code Online (Sandbox Code Playgroud)

在哪里checkme():

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)
    }
}  
Run Code Online (Sandbox Code Playgroud)

如果我运行它,它冻结Firefox 19没有错误消息.但是如果我用第一个参数替换(在代码和checkme()函数中):

st=setTimeout(function(){checkme()},4000)
Run Code Online (Sandbox Code Playgroud)

它工作正常.所以我的问题是:checkme()使用或不使用anon函数调用函数有什么区别?为什么在第一种情况下会冻结Firefox?

谢谢

javascript settimeout

7
推荐指数
2
解决办法
5277
查看次数

标签 统计

javascript ×1

settimeout ×1