...

View Full Version : Help with setTimeout();



justincredible
08-31-2006, 02:27 PM
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...


<p>
<script>
<!--
getLyric();
//-->
</script>
</p>


Here is the function...

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!

justincredible
08-31-2006, 02:37 PM
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.:mad:

A1ien51
08-31-2006, 03:04 PM
You can not use document.write after the page loads you need to use document.getElementById("someId").innerHTML

Eric

justincredible
08-31-2006, 03:48 PM
Thanks for the reply. I am finding it difficult to apply that to my code, however.

Here is what I tried:



<p id="lyric">
<script>
<!--
getLyric();
//-->
</script>
</p>


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);

}

A1ien51
08-31-2006, 04:44 PM
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

justincredible
08-31-2006, 04:58 PM
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?

A1ien51
08-31-2006, 05:43 PM
call the function like you were before

Eric



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum