Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2012, 03:03 PM   PM User | #1
wylie233
New Coder

 
Join Date: Jul 2012
Location: UK
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
wylie233 is an unknown quantity at this point
Why can't my program be random!

Hello!


i have a problem, which is i can not get this code right:

Code:


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

var body2 = document.getElementById("body2");
var talk_sen = ["", "did","do", "not"];
var ran_talk_sen = talk_sen[Math.floor(Math.random() * talk_sen.length)];


setInterval(function(){document.write(ran_talk_sen)},3000);

</script>
</head>
<body>






</body>

</html>




SO the idea is, every 3 seconds if prints out a random element of the array, this does not quite work because
it only prints out 'do' then after 3 secs it prints 'do' etc, until my page says dodododododododododododododododododo.

i am hoping for diddonotnotnotdiddodidnotdodonotdiddid - or something random like that!



Thanks!!!
wylie233 is offline   Reply With Quote
Old 07-21-2012, 03:50 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,358
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<body>
<html>
<head>
document.write can not be used after the page has loaded
<div id="body2"   ></div>
<script type = "text/javascript">

var body2 = document.getElementById("body2");
var talk_sen = ["", "did","do", "not"];
var ran_talk_sen = talk_sen[Math.floor(Math.random() * talk_sen.length)];


setInterval(function(){ body2.innerHTML=talk_sen[Math.floor(Math.random() * talk_sen.length)]; },3000);

</script>
</head>
<body>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 07-21-2012, 04:02 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Seems a bit pointless, but I expect it is homework.

document.write() is in effect obsolete. 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 (including the Javascript which called it). 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.

So you must use DOM methods to display the result.

Code:
<body onload ="myfunc()">
<div id = "result"></div>
<script type = "text/javascript">
function myfunc() {
var talk_sen = ["", "did","do", "not"];
var ran_talk_sen = talk_sen[Math.floor(Math.random() * talk_sen.length)];
document.getElementById("result").innerHTML += ran_talk_sen + "<br>";
setTimeout(myfunc,2000);
}
</script>
But you have no way of bringing it to an end!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
wylie233 (07-22-2012)
Old 07-22-2012, 12:19 PM   PM User | #4
wylie233
New Coder

 
Join Date: Jul 2012
Location: UK
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
wylie233 is an unknown quantity at this point
Thanks! Yes my example is pointless, but i am using the same method for something else!

Document.write does clear the page - i know this, but it was just to do a quick check for if it worked!!

Thanks again!
wylie233 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:03 PM.


Advertisement
Log in to turn off these ads.