-1

Here is my page script:

<?php
session_start();
$_SESSION["square"]=$_SESSION["square"]+$_POST['roll'];
echo '<img src="images/dice.gif">';
?>
<?php sleep(10);?>
<?php
echo "You rolled a ".$_POST['roll']."!";   
if ($_SESSION["square"]<100){
    $url="board.php";
}
else
{
  $url="finished.php";
}
 echo '<META HTTP-EQUIV=Refresh CONTENT="2; URL='.$url.'">'; 
?>

The 'sleep' happens before the content of my page loads. I want the pause to happen before the dice roll result appears but after the gif is displayed.

Why is the pause occurring seemingly before it's called?

stilts77
  • 191
  • 3
  • 14
  • presumably because of output buffering. Try "ob_flush" before the sleep()... – Honk der Hase Jul 25 '16 at 13:16
  • 1
    Ultimately you're depending on a variety of factors here for what you want to achieve, not least of all that the browser will render a half-finished HTML document. That's not really how it works/how it's guaranteed to work. You should achieve your effect by explicitly delaying the display in Javascript client-side; not by counting on the timing of the delivery of half an HTML page. – deceze Jul 25 '16 at 13:18
  • I can't provide an answer since this was closed but create an `index.php` page and add this code: `switch($_GET['page']){case 'result':echo 'You rolled a '.rand(1, 6).'!';echo '

    Roll again';break;case 'rolling_animation':echo '';echo ''; break;default:echo '

    Roll';break;}`
    – MonkeyZeus Jul 25 '16 at 13:32

2 Answers2

0

PHP is executed on the server-side. Letting the user wait (sleep) with PHP will probably result in a bad user experience, since the website will still appear to be loading..

You could try to calculate the dice roll in PHP, and display it with HTML or JavaScript depending on your needs, and forward the user afterwards.

damian
  • 2,001
  • 20
  • 38
0

Usually PHP will wait for the entire script to finish executing before sending the output to your HTTP server and client.

However ob_flush() exists to "send the contents of the output buffer", so you can try that.

But it must be noted PHP and webpages are not designed for this start/stop kind of transmission - AJAX would be a better option.

bnx
  • 417
  • 5
  • 11