delickate Posted August 28, 2009 Share Posted August 28, 2009 hi, i need to stay a loop for 3 sec. e.g i've following loop: for($i =0; $i <=10; $i++) { echo $i."=sani<br>"; sleep(3); } i want to show first 0 then it should wait for 3 sec then print 1 ans again stay for three sec and then print 2 and stay for 3 sec and so on.... so my out put show show like this 0=sani ( stay for three sec) 1=sani ( stay for three sec) 2=sani ( stay for three sec) 3=sani ( stay for three sec) 4=sani ( stay for three sec) 5=sani ( stay for three sec) 6=sani ( stay for three sec) 7=sani ( stay for three sec) 8=sani ( stay for three sec) 9=sani ( stay for three sec) 10=sani ( stay for three sec) can any body help me plz Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/ Share on other sites More sharing options...
ram4nd Posted August 28, 2009 Share Posted August 28, 2009 you can do this with javascipt on client side Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908117 Share on other sites More sharing options...
delickate Posted August 28, 2009 Author Share Posted August 28, 2009 can you post the example code plz Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908118 Share on other sites More sharing options...
play_ Posted August 28, 2009 Share Posted August 28, 2009 can you post the example code please google "javascript sleep function" it doesn't have one by default, but people have made a work around. Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908121 Share on other sites More sharing options...
Psycho Posted August 28, 2009 Share Posted August 28, 2009 google "javascript sleep function" it doesn't have one by default, but people have made a work around. What about setTimeout()? He really needs a loop that executes every three seconds as opposed to pausing the execution. <html> <head> <script type="text/javascript"> function countToTen(num) { var ouputObj = document.getElementById('output'); var newLine = num + '=sani<br />'; ouputObj.innerHTML = ouputObj.innerHTML + newLine; if (num<10) { num++; setTimeout('countToTen('+num+')', 3000); } } </script> </head> <body onload=countToTen(0);> <div id="output"></div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908128 Share on other sites More sharing options...
delickate Posted August 28, 2009 Author Share Posted August 28, 2009 thanks for help me out. I've tried this code. but it works only for client side script. i need to wait php loop for 3 sec. if i used javascript function setTimeout it doesn't work the way that i want. is there any php function? Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908130 Share on other sites More sharing options...
oni-kun Posted August 28, 2009 Share Posted August 28, 2009 You can use sleep, but you must use flush and ob_flush with it I believe.. look at sleep's examples and there should be a tutorial on using the output buffer to sleep, and THEN display the buffer for the next part. EDIT: I found my working code! Try something with this.. if (ob_get_level() == 0) ob_start(); echo "I am the first"; echo str_pad('',4096)."\n"; ob_flush(); flush(); sleep(3); echo "I am the second, 3s later."; echo str_pad('',4096)."\n"; ob_flush(); flush(); sleep(3); ob_end_flush(); Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908133 Share on other sites More sharing options...
delickate Posted August 28, 2009 Author Share Posted August 28, 2009 Thanks a lot oni-kun. it works fine. Thank you very much. mmm...ah Quote Link to comment https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908140 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.