3

I'm having trouble figuring what's wrong with this piece of code. It runs once in chrome then crashes then runs and so on. It can't run in firefox and 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>

Am i missing something important in javascript? what am i doing wrong??

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

Eric
  • 75
  • 6
  • You needed a return before the tag :) – Rich Bradshaw Oct 17 '11 at 10:26
  • 1
    Using document.write after the page is loaded will cause problems. http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – BNL Oct 17 '11 at 12:42
  • @BNL : yeah, that was stupid from me while i knew that : http://javascript.crockford.com/script.html – Eric Oct 17 '11 at 12:49

1 Answers1

1

I get the error

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

when I run this in firefox. This occurs when it tries to invoke fn() inside AsyncRunner.wait.

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

I could not reproduce the crash of the program on chrome.

Community
  • 1
  • 1
Narendra Yadala
  • 9,110
  • 1
  • 24
  • 43
  • Thanks! As a matter of fact, the crash in Chrome is for version 5 and downwards.please see edit. And you are right: that's the problem: document.write()! By reading provided answers, it seems that happens with badly written code: what's bad with the code i wrote? to sum up, i wrote a custom function that uses Dom and replace document.write() with it. it works now! please see question for update – Eric Oct 17 '11 at 12:35
  • I don't think that there is something wrong with your code, it is a bug in the implementation of the browsers. – Narendra Yadala Oct 17 '11 at 12:40