Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,503
    Thanks
    3
    Thanked 500 Times in 487 Posts
    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/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,912
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • Users who have thanked Philip M for this post:

    wylie233 (07-22-2012)

  • #4
    New Coder
    Join Date
    Jul 2012
    Location
    UK
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •