English 中文(简体)
随时间推测
原标题:Running asynchronously with timers

我担心的是,这部法典有什么错误。 飞机一度在冰箱中运行,然后坠毁。 它可以在火ox和电离层中运行。

<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>

i 失踪在javascript中很重要? 什么是错?

UPDATE The code runs in the following browsers: Chrome 14.08 - Safari 5.1 It will also run in JsFiddle.net as expected. It will work but crash repeatedly in Chrome 5.0 It stops when it has to run functions containing SyncRunner.wait() in the following browsers - without issuing any error : Firefox 3.6 - IE 9 - Opera 10.61

UPDATE 2 After a little bit investigation with the answer provided by Narenda, it seems FF 4 and onwards are having trouble here. But i don t the underlying reason with other browsers. So the problem is using document.write() in non-webkit browsers Replace document.write() with the following function and it should work by now:

function print( message ) {
    var loc = document.getElementById(  logpanel  );
    var tag = document.createElement( li );
    if( message != undefined ){
        tag.appendChild( document.createTextNode( message ) );
        loc.appendChild( tag );
    }
}

I m still interested in know what went wrong with other browsers Thanks

问题回答

我发现错误

attempt to run compile-and-go script on a cleared scope

当我以火ox为主时。 当它试图在<条码>内援引<>fn(>时,即发生这种情况。

Looks like this is a bug in firefox 4 and onwards. Have a look at these related SO questions Error: Attempt to run compile-and-go script on a cleared scope (specifically the last answer talks about using document.write) and jQuery Animation error = attempt to run compile-and-go script on a cleared scope

我不能再照搬神学院的坠毁。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签