1

Possible Duplicate:
setInterval not working (firing only once) in Google Chrome extension

This error only is a problem when document.write() is used.

I am on Mac 10.6.8 Intel.

Here is my script:

setInterval(function(){myFun()},1000);

function myFun() {
   document.write('something');
}

The behavior is different in various browsers: Firefox 12: Only happens once. The browser says "connecting" in the status bar. Google Chrome and safari: Seems to work correctly.

Note: using setTimeout instead causes everything to behave like firefox (not working). Using setInterval(myFun},1000), which is supposedly the source of so much error, behaves identically.

Community
  • 1
  • 1
Kevin Kostlan
  • 3,311
  • 7
  • 29
  • 33

4 Answers4

1

Many beginning tutorials use document.write() for "hellow world". However, this funciton is dangerous because it may mess up the script (by nuking the entire program). Here is a safe way to do debug printouts:

Before the script, in between the and in the html, add this:

<div id="myDebug"></div>

In the script, first convert it to a variable that can be called upon:

var myDebug = document.getElementById("myDebug");

When you need to show something, do this:

debug.innerHTML = "some string";

This will show the string in the browser window.

Kevin Kostlan
  • 3,311
  • 7
  • 29
  • 33
0

Seems that you miss the semicolon after myFun()

setInterval(function(){myFun();},1000);

function myFun() {
   document.write('something');
}

Also make sure you put the function in <body></body>

NSF
  • 2,499
  • 6
  • 31
  • 55
  • That semicolon is not syntactically significant here. – apsillers Jun 25 '12 at 19:37
  • So many people think that the setInterval() syntax is causing problems when the stupid document.write(), which many tuts recommend using, is the real problem. – Kevin Kostlan Jun 25 '12 at 19:39
  • Well I've never used document.write(). What you say may be the crux of his problem. Good call. – NSF Jun 25 '12 at 19:42
0

document.open() results in clearing the document. This function gets called by Firefox before document.write(). Thus your interval is lost.

see W3C Document Object Model HTML

Nils Blum-Oeste
  • 5,608
  • 4
  • 25
  • 26
0

try this:

setInterval(function(){myFun()},1000);

function myFun() {
   // Return all elements in document of type 'body'
   // (there will only be one)
   var bodyArray = document.getElementsByTagName('body');
   // get the body element out of the array
   var body = bodyArray[0];
   // save the old html of the body element
   var oldhtml=body.innerHTML;
   // add something to the end
   body.innerHTML=oldhtml+"somrthing";
}

As others have mentioned, using document.write() will (in some browsers) nuke the script used to update the document, writing to the body (and not the head, where the javascript should be stored) will stop this from happing.

Waltzy
  • 1,113
  • 4
  • 14
  • 31
  • Correct, my answer says the same thing essentially. – Kevin Kostlan Jun 25 '12 at 19:38
  • 1
    Yea I believe innerHTML always works better. – NSF Jun 25 '12 at 19:43
  • And the browsers where it does NOT nuke the script aren't following the spec. In particular, document.write after the document is done parsing should clear all pending timers (including the one involved here), per spec, but doesn't do so in some WebKit-based browsers. – Boris Zbarsky Jun 26 '12 at 03:35