samoi Posted November 29, 2009 Share Posted November 29, 2009 Hello guys I need to get something fun! with setTimeout function! I am n00b! 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 https://forums.phpfreaks.com/topic/183274-help-with-settimeout/ Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 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 https://forums.phpfreaks.com/topic/183274-help-with-settimeout/#findComment-967378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.