Jump to content

help with setTimeout!


samoi

Recommended Posts

Hello guys

 

I need to get something fun! with setTimeout function!

I am n00b! :D so be patent please.

 

I need when <body onload="Myfunc();"> fires, that function should show "Please wait...!" or "Loading...". for , say 5 seconds!. then it disappear.

 

I used setTimeout with that but it didn't do what I wanted!

 

here is my code:

 

function Myfunc(){
document.getElementById("ss2").innerHTML = "Loading...";
setTimeout("Myfunc();", 5000);
}

// where id="ss2" is the place to display the string "loading..." !

 

 

 

and a one more question !

 

can I do something like a while loop! where it holds *i* value as seconds!

and for every second it passes it should print out a string I make it up !

 

let say loop for 3 seconds !

So, when seconds = 0 then display "string of second 0" and stick it in there, then go to the next second when it is exactly = 1 then display "new line with string of second 1" and stick it in there, then do to the last second when it is exactly = 2 then display " new line with string of second 2" and stick it in there, exit the loop!

 

 

is there anyway to do that? please note that I am a n00bie !

 

help is much appreciated !

 


Link to comment
Share on other sites

You mean something like this?

 

<script type="text/javascript">
var customMessages = new Array('Message 1', 'Message 2', 'Message 3', 'Message 4', 'Message 5');
var secondsPassed = 0;
function myFunc()
{
var t = setTimeout("load()", 1000);
}

function load()
{
document.getElementById("ss2").innerHTML = 'Loading..' + customMessages[secondsPassed];
if(++secondsPassed < 6)
	var t = setTimeout("load()", 1000);
else
	document.getElementById("ss2").innerHTML = 'Loaded!';
}
</script>

<body onload="myFunc()">
<div id="ss2">Loading..</div>
</body>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.