Javascript 与计时器异步运行

Javascript 与计时器异步运行,javascript,timer,Javascript,Timer,我很难找出这段代码的错误。它在chrome中运行一次,然后崩溃,然后运行,等等。它不能在firefox和IE中运行 <html> <head> <title> Async Module Runner </title> <script type="text/javascript"> var TestRunner = (function(){ var queue = []; var paused = false; return {

我很难找出这段代码的错误。它在chrome中运行一次,然后崩溃,然后运行,等等。它不能在firefox和IE中运行

<html>
<head>
<title> Async Module Runner </title>
<script type="text/javascript">

var TestRunner = (function(){
var queue = [];
var paused = false;

return {
    run : function( fn ) {
        queue.push(fn);
        if( queue.length > 0 &&  paused != true && queue[0] != undefined ) {
            queue.shift()();
        }
    },

    pause : function(){
        paused = true;
    },

    resume : function(){
        paused = false;
        this.run();
    }
};
})();   

var  AsyncRunner = {};
AsyncRunner.wait = function( fn, time ) {
TestRunner.pause();
setTimeout(function(){
    fn();   
    TestRunner.resume();
}, time);
};

var Test = {
setUp : function(){
    document.write('yep! <br />');
},

tearDown : function(){
    document.write('yep yep <br />');
},

testAsync1 : function(){
  AsyncRunner.wait( function(){ document.write('okay! <br />'); }, 3000 );
},

testAsync2 : function(){
  AsyncRunner.wait( function(){ document.write('okay again <br />'); }, 2000 );
},

testAsync3 : function(){
  AsyncRunner.wait( function(){ document.write('okay again and again!! <br />'); }, 5000            );
}
};

window.onload = function(){
for( var i in Test ) {
    TestRunner.run(Test[i]);
}
};
</script>
</head>
<body>
</body>
</html>
我仍然想知道其他浏览器出了什么问题 谢谢

我知道了错误

'尝试在清除的作用域上运行编译并执行脚本'

当我在firefox中运行它时。当它尝试在
AsyncRunner.wait中调用
fn()
时,会发生这种情况

看起来这是Firefox4及以后版本中的一个bug。看看这些相关的SO问题 (特别是最后一个答案谈到如何使用
document.write
)和


我无法在chrome上重现程序崩溃。

您需要在标记之前返回:)加载页面后使用document.write将导致问题@BNL:是的,当我知道这一点时,我真是太蠢了:谢谢!事实上,Chrome中的崩溃适用于5版及以下版本。请参见编辑。你是对的:这就是问题所在:document.write()!通过阅读提供的答案,似乎写得不好的代码会发生这种情况:我写的代码有什么不好?总之,我编写了一个自定义函数,使用Dom替换document.write()。现在可以了!请参阅更新问题。我不认为您的代码有问题,这是浏览器实现中的错误。
function print( message ) {
    var loc = document.getElementById( 'logpanel' );
    var tag = document.createElement('li');
    if( message != undefined ){
        tag.appendChild( document.createTextNode( message ) );
        loc.appendChild( tag );
    }
}