Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
How do I create a pause in the execution of the code

I am wanting to create a list that reveals itself slowly on the screen, but it seems that all routines are completed before they present themselves.

For example:

for ($x = 0; $x <= 25; $x++)
{
 echo "The number is: $x <br>";
 sleep(1);
}

In my mind this should display "The number is 1", wait 1 second then display "The number is 2". In reality is waits 25 seconds and shows all the numbers, except there seems to be an error there too as it only actually displays the first 10 !!!

EDIT: I also tried "usleep" which had the same effect

I'm not concerned with the error, but how do I add some waiting please?

EDIT: Following on, I am still getting the same issue with this code.

 for ($x = 1; $x < 5; $x++)
 {
  $st=$_SESSION['st']=$x;
  echo '<iframe class="findlist" name="iframefindlist" id="findlist" src="findmelist.php"></iframe>';
  ob_flush();
  flush();
  usleep(3000000);
 }

And then the "findmelist.php" is

<body>
<?php
$st=$_SESSION['st'];
$conn = new mysqli("localhost", "acc", "pw", "db");

?>
<div class="findlist1">
</div>

<div class="findlist2">
 <?php

 $result = mysqli_query($conn,"SELECT * FROM tablename WHERE number='$st'");
 while($row = mysqli_fetch_array($result))
 {
  $fname=$row['fname'];
  $lname=$row['lname'];
 }
 echo $fname.' '.$lname;
 ?>
</div>

<div class="findlist3">
</div>

</body>
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to flush the output_buffer first, otherwise the answer is only sent when the execution has finished.

ob_flush();
flush();

So for your code, that means:

for ($x = 0; $x <= 25; $x++)
{
 echo "The number is: $x <br>";
 ob_flush();
 flush();
 sleep(1);
}

For more info, see the following reference.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!