5

is it possible to use sleep() (or some other function) to wait before execution?

I have for example:

<div>bla bla</div>
<?php
$a
echo $a; ?>

some divs and html
<?php
$b
echo $b; ?>

How to execute the first php script say 5 sec after the page has loaded but to show everything else as the page loads? If I use sleep() before the first php it will delay the whole page from loading.

Gorna-Bania
  • 215
  • 1
  • 2
  • 9
  • 4
    Not to get technical and pedantic, but *after the page has loaded* also coincides with *after the PHP script has finished executing*. You can delay the script with sleep, but it will appear to the user as though the page is still loading even if you do things like flush the output buffer. This may be a less than ideal user experience. I would consider using JavaScript instead. – apokryfos Oct 13 '16 at 10:44
  • 2
    php is a server side language. So it will be completely executed before the page is sent to the browser. To get your goal you can use Ajax. You load the page and then call for the php part you want when you want (after page load). Ajax is async. – Lelio Faieta Oct 13 '16 at 10:45

3 Answers3

3

Yes, you can use sleep() to delay execution, but since the output is usually buffered and isn't sent to the browser until the script has finished the result isn't what you're after.

If you do a flush() and ob_flush() right after calling sleep() the output buffer is flushed and sent to the browser.

See http://php.net/manual/en/function.ob-flush.php

Ryan
  • 1,151
  • 5
  • 7
Janek
  • 2,942
  • 1
  • 14
  • 14
  • 2
    You don't want to do this. It is up to the client/browser to decide if rendering should be started right away or only when the whole document has been received. Worst case is, the user sees nothing until the whole document has been received by the browser. – Ulrich Thomas Gabor Oct 13 '16 at 10:47
  • 1
    I second this. For doing things like that, you want to use JavaScript. – Niklas S. Oct 13 '16 at 10:51
  • 1
    Indeed, it's a very bad idea to try to delay page execution for this reason. My answer was from a pure "how to" standpoint. :) – Janek Oct 13 '16 at 10:53
2

You want to use AJAX for this. Example using jQuery:

<div id="content">
<-- You want to load something here after 5 seconds -->
</div>

<script type="text/javascript">
    setTimeout(function() {
        $('#content').load("/url/to/your/script.php");
    }, 5000); // 5000 is the time to wait (ms) -> 5 seconds
</script>

This would load the output (echo) of script.php into the div with the ID "content". By doing it this way, you let the client do all the rendering work as already recommended by @GhostGambler

This will not block outputting other content sent by the initial script.

Niklas S.
  • 1,046
  • 10
  • 22
0

The HTTP protocol includes a special header, Transfer-Encoding, that allows you to specify that the page could be loaded in chunks (Transfer-Encoding: chunked), although there is no guarantee that the browser will do so (but most browsers will).

The Transfer-Encoding: chunked header signals to the browser that the web server does not know the Content-Length in advance.

While typically you want to use Ajax to load content after that page has been fully loaded, you can use this technique to show, for example, realtime terminal output, without needing JavaScript.

See this question on how to implement chunked responses in PHP.

Community
  • 1
  • 1
Pieter van den Ham
  • 4,381
  • 3
  • 26
  • 41