Jump to content

stay loop for 3 sec


delickate

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908128
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/172234-stay-loop-for-3-sec/#findComment-908133
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.