开发者

Delay PHP echo, without halting the page?

开发者 https://www.devze.com 2023-03-10 21:45 出处:网络
I want to draw text after 1 second delays, without halting the page load for the duration of all of the delays in total.

I want to draw text after 1 second delays, without halting the page load for the duration of all of the delays in total.

Does anyone know how?

I have the below, but rather than drawi开发者_JS百科ng the TIME and "Hello", then waiting a second and drawing "Goodbye", followed by another second and finally "The End".... It waits 2 seconds and then draws everything.

<?php

echo date('H:i:s');
echo "<br>";
echo 'Hello';
echo "<br>";
sleep(1);
flush();

echo 'Goodbye';
echo "<br>";
sleep(1);
flush();

echo 'The End';
?>


As PHP is executed server-side, the user won't get the output until the script is finished executing. To add a visible delay of outputting messages, you would have to use JavaScript or another Client-Side executed scripting language.


You can use jQuery in somewhat this way: (Assuming that your text is initialised with hello)

 $('#id-of-your-tag-containing-text').fadeOut(500, function() {
       $('#id-of-your-tag-containing-text').html("GoodBye!");
       $('#id-of-your-tag-containing-text').fadeIn(500);
       $('#id-of-your-tag-containing-text').fadeOut(500, function() {
              $('#id-of-your-tag-containing-text').html("The End!");
        });
       $('#id-of-your-tag-containing-text').fadeIn(500);
}); //Note that the number 500 represents the time delay in milliseconds.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号