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