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 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2006
    Location
    Cincinnati, OH
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with setTimeout();

    I have a script that I want to display a lyric from a song and then change the lyric every so many seconds. Problem is I can't seem to get it to work.

    Here is what I have:

    Here is where I call the function...
    Code:
    <p>
       <script>
          <!--
             getLyric();
          //-->
       </script>
    </p>
    Here is the function...
    Code:
    function randomNumber()
    {
        var ranNum= Math.floor(Math.random()*4);
        return ranNum;
    }
    
    function getLyric()
    {
       var number=randomNumber();
    
        var lyric=new Array(4);
         lyric[0]="And if I lived a thousand years you know I never could explain<br>The way I lost my heart to you that day...";
         lyric[1]="I love you ‘cause I know that I can’t do anything wrong<br>You’re where I belong like red on a rose...";
         lyric[2]="And I'm gonna make you a promise if there's life after this<br>I'm gonna be there to meet you with a warm, wet kiss...";   
         lyric[3]="In your eyes I see forever<br>Makes me wish that my life never knew<br>The day before you...";
      
       var that = setTimeout(document.write(lyric[number]), 5000);
       
    }
    It prints out the lyrics and will change them when the page is refreshed but won't change them otherwise.

    I know it's probably something easy, but I am a dolt when it comes to javascript.

    Thanks!

  • #2
    New Coder
    Join Date
    Jul 2006
    Location
    Cincinnati, OH
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm thinking I need to run the setTimeout() on getLyric but I tried that last night and couldn't get it working at all.

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You can not use document.write after the page loads you need to use document.getElementById("someId").innerHTML

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    New Coder
    Join Date
    Jul 2006
    Location
    Cincinnati, OH
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the reply. I am finding it difficult to apply that to my code, however.

    Here is what I tried:

    Code:
    <p id="lyric">
       <script>
       <!--
    	getLyric();
       //-->
       </script>
    </p>
    Code:
    function randomNumber()
    {
        var ranNum= Math.floor(Math.random()*4);
        return ranNum;
    }
    
    function getLyric()
    {
       var number=randomNumber();
    
        var lyric=new Array(5);
         lyric[0]="And if I lived a thousand years you know I never could explain<br>The way I lost my heart to you that day...";
         lyric[1]="I love you ‘cause I know that I can’t do anything wrong<br>You’re where I belong like red on a rose...";
         lyric[2]="And I'm gonna make you a promise if there's life after this<br>I'm gonna be there to meet you with a warm, wet kiss...";   
         lyric[3]="In your eyes I see forever<br>Makes me wish that my life never knew<br>The day before you...";
      
       var that = setTimeout(document.getElementById("lyric").innerHTML = (lyric[number]), 500);
       
    }

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    this is wrong, settimeout uses a string, you are not giving it a string

    var that = setTimeout(document.getElementById("lyric").innerHTML = (lyric[number]), 500);



    You should be using setInterval in my eyes.

    var timer = null;
    window.setInterval("getLyric()",500);


    inside the function just do
    document.getElementById("lyric").innerHTML = lyric[number];

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    New Coder
    Join Date
    Jul 2006
    Location
    Cincinnati, OH
    Posts
    85
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Awesome, that does pretty much what I want it to do. The only thing is it doesn't display the lyrics until it goes through the interval one time. Is there something I need to add to get this to display right away?

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    call the function like you were before

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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