...

View Full Version : setInterval question



syco__
12-14-2011, 03:20 AM
Hey guys,

Im playing around with JS trying to learn more and i have come across this bit of code


<script type="text/javascript">

function print () {
document.write("Words ");
}

setInterval("print()", 1000);

</script>

Just having problems getting it to display correctly in all browsers its displaying the first Words fine in all but will only continue to reprint "Words" in chrome.

Thanks any help tips ideas would be great.

low tech
12-14-2011, 03:52 AM
Hi

I think because you are using document.write which is used only once on page load.


try this using innerHTML --


<div id="words"></div>
<script type="text/javascript">

var count = 0;
function print (){
if (count < 10 ){
var ele =document.getElementById("words");
ele.innerHTML += "words<br />"; //add one word
count++;
}
}
setInterval("print()", 1000);

</script>

LT

Old Pedant
12-14-2011, 04:09 AM
Yep, when you use document.write after a page is loaded, it *WIPES OUT* the entire page, including even the JavaScript code that did the write!

And by definition, the code executed by setInterval will not execute until the page is indeed fully loaded. Ergo...one document.write and you are all done. Kablooey.

******

I love your "It's only simple if...", low tech, but there are many even easier ways to make JavaScript code obscure. <grin/>

syco__
12-14-2011, 05:10 AM
frustrating when dealing with multiply browsers. Is this a known site of all compatibility issues with each browser that lists what does and doesn't work?

Thanks for your quick reply's :)

Old Pedant
12-14-2011, 07:02 AM
??? This one is *NOT* browser dependent. ALL browsers work this way.

Here's a hint for you: If at all possible, don't use document.write. If you *must* use it, only use it in-line in your HTML, between the <body> and </body> tags, and never in response to any kine of human interaction or a timer.

Philip M
12-14-2011, 07:23 AM
To re-inforce the message:-


document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

syco__
12-14-2011, 07:32 AM
but my code does works fine in chrome every second it does what the function instructs ie prints "Words" but none the less ill be trying to learn more about innerHTML havn't had much to do with it as of yet.

Thanks for your detailed replies

Old Pedant
12-14-2011, 07:45 AM
Chrome is, I think, wrong in what it does.

However...

If you try it again and have *ANY* other content in the <body> of your page, you will see that even Chrome indeed wipes out everything else except what the document.write is doing.

Example.


<html>
<head>
<script type="text/javascript">

function print () {
document.write("Words ");
}

setInterval("print()", 1000);

</script>
</head>
<body>
Here is the body. It will be wiped out by the first write. Yes, even with Chrome.

</body>
</html>

rnd me
12-14-2011, 07:52 PM
print() is a native global in chrome and others...

Philip M
12-14-2011, 08:09 PM
print() is a native global in chrome and others...

As you say, the window.print() method is supported in all major browsers. So a function named print() will clash with it.

You avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'test' or 'text' or 'checked' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.
And of course you may not give a variable a name which is a Javascript keyword or event such as alert, case, char, confirm, false, int, null, onload, return, this, void, and so on.

felgall
12-14-2011, 08:39 PM
Teh other thing wrong with the original code is that it is passing a string as the first parameter to the setInterval where that function expects to be passed a function as the first parameter. This means that it has to convert the string to a function every time it needs to use it. Since the string contains a function call anyway it is easy to do away with the conversion simply by recoding it as

setInterval(print, 1000);

Philip M
12-15-2011, 07:35 AM
Teh other thing wrong with the original code is that it is passing a string as the first parameter to the setInterval where that function expects to be passed a function as the first parameter. This means that it has to convert the string to a function every time it needs to use it. Since the string contains a function call anyway it is easy to do away with the conversion simply by recoding it as

setInterval(print, 1000);

Was this not necessary in old versions of IE? <IE5? But the habit dies hard!

Old Pedant
12-15-2011, 07:41 AM
I certainly learned it first as you-must-use-a-string, but who knows whether we were taught wrong or it was really necessary. I don't have MSIE 5 or so to play with any more.

Philip M
12-15-2011, 07:44 AM
Teh other thing wrong with the original code is that it is passing a string as the first parameter to the setInterval where that function expects to be passed a function as the first parameter. This means that it has to convert the string to a function every time it needs to use it. Since the string contains a function call anyway it is easy to do away with the conversion simply by recoding it as

setInterval(print, 1000);

Was this not necessary in old versions of IE? <IE5? But the habit dies hard!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum