PDA

View Full Version : Setting timer on message



lendila
03-26-2007, 02:25 PM
Hi guys!

I need a code that displays a message after a duration of 3 seconds, I have an idea of code organization but I can't make it work coz I dont know some of the things!!!!

var visitorMsg1 = "Welcome visitor 1";
var visitorMsg2 = "Welcome visitor 2";

var durationTime = 0;

if(durationTime == 3){
document.write(visitorMsg1);
}
else{
document.write(visitorMsg2);
}

Veronica
03-26-2007, 03:00 PM
You can use setTimeout to make something happen after a given period of time. The number reflects milliseconds, so 3000 equals 3 seconds.
Also, using innerHTML will give you more flexibility than document.write, since document.write will re-write the entire page.




<html>
<head>
<script type="text/javascript">
var visitorMsg1 = "Welcome visitor 1";
var visitorMsg2 = "Welcome visitor 2";
setTimeout('message2()', 3000);
function message2()
{
document.getElementById('message2').innerHTML = visitorMsg2;
}
</script>
</head>
<body>
<p id="message2">
<script type="text/javascript">
document.write(visitorMsg1);
</script>
</p>
</body>
</html>


If you want to keep message 1 and add message 2, modify it like this


<p id="message1">
<script type="text/javascript">
document.write(visitorMsg1);
</script>
</p>
<p id="message2">
</p>

lendila
03-27-2007, 06:10 AM
Hi Veronica,

Thanks a lot u have solved a huge problem I had. now I can go ahead with my application.....

thanx once again...
Willy