PHP Doku:: Programmverzögerung - function.sleep.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenMiscellaneous FunctionsSonstige Funktionensleep

Ein Service von Reinhard Neidl - Webprogrammierung.

Sonstige Funktionen




(PHP 4, PHP 5)



int sleep ( int $seconds )

Unterbricht die weitere Skript-Ausführung für die angegebene Anzahl von Sekunden.



Die Unterbrechung in Sekunden


Gibt bei Erfolg null oder im Fehlerfall FALSE zurück. Wenn der Aufruf durch ein Signal unterbrochen wurde, gibt sleep() die Anzahl der Sekunden zurück, die das Programm noch "schlafen" würde.


Falls die angegebene Anzahl von Sekunden negativ ist, erzeugt diese Funktion einen Fehler des Typs E_WARNING.


Beispiel #1 sleep()-Beispiel

// die aktuelle Zeit
echo date('h:i:s') . "\n";

// 10 Sekunden schlafen

// aufwachen!
echo date('h:i:s') . "\n";

Dieses Beispiel erzeugt nach zehn Sekunden die Ausgabe


Siehe auch

11 BenutzerBeiträge:
- Beiträge aktualisieren...
soulhunter1987 at post dot ru
18.08.2010 9:49
Since sleep() can be interrupted by signals i've made a function which can also be interrupted, but will continue sleeping after the signal arrived (and possibly was handled by callback). It's very useful when you write daemons and need sleep() function to work as long as you 'ordered', but have an ability to accept signals during sleeping.

function my_sleep($seconds)
$start = microtime(true);
    for (
$i = 1; $i <= $seconds; $i ++) {
time_sleep_until($start + $i);
toddjt78 at msn dot com
10.05.2010 4:39
Simple function to report the microtime since last called or the microtime since first called.

function stopWatch($total = false,$reset = true){
$now_time = microtime(true);
    if (
$last_called === null) {
$last_called = $now_time;
$first_called = $now_time;
    if (
$total) {
$time_diff = $now_time - $first_called;
    } else {
$time_diff = $now_time - $last_called;
    if (
$last_called = $now_time;

$reset  - if true, resets the last_called value to now
$total - if true, returns the time since first called otherwise returns the time since last called
jimmy at powerzone dot dk
6.03.2010 17:26
Notice that sleep() delays execution for the current session, not just the script. Consider the following sample, where two computers invoke the same script from a browser, which doesn't do anything but sleep.

PC 1 [started 14:00:00]: script.php?sleep=10 // Will stop after 10 secs
PC 1 [started 14:00:03]: script.php?sleep=0 // Will stop after 7 secs

PC 2 [started 14:00:05]: script.php?sleep=0 // Will stop immediately may be used to address this problem.
mohd at Bahrain dot Bz
16.12.2009 10:12
I hope this code will help somebody to solve the problem of not being able to flush or output the buffer to the browser (I use IE7).
It may work for you with just [ echo str_repeat(".", 4096); ] and without even using ob_... and flush.


//[ OR ] echo "..."; ob_flush(); flush();


sleep_echo($secs) {
$secs = (int) $secs;
$buffer = str_repeat(".", 4096);
//echo $buffer."\r\n<br />\r\n";
for ($i=0; $i<$secs; $i++) {
date("H:i:s", time())." (".($i+1).")"."\r\n<br />\r\n".$buffer."\r\n<br />\r\n";


f dot schima at ccgmbh dot de
10.11.2009 15:48
Remember that sleep() means "Let PHP time to do some other stuff".
That means that sleep() can be interrupted by signals. That is important if you work with pcntl_signal() and friends.
8.02.2009 21:32
This will allow you to use negative values or valuer below 1 second.

<?php slaap(0.5); ?>

function slaap($seconds)
$seconds = abs($seconds);
    if (
$seconds < 1):
webseos at gmail dot com
27.08.2008 6:29
This is a critical thing to use time delay function as sleep() Because a beginner can find that this is not working and he/she will see that all output appearing at a time.

A good way to implement this is by using the function -  ob_implicit_flush() then you don't need to use flush() function explicitly.

A sample code :
<div style="width:200px; background-color:lime;border:1px; text-align:center;text-decoration:blink;">

marpetr at gmail dot com
18.03.2008 8:41
Very useful to prevent password brute forcing! Simply add few seconds timeout to login script and the probability to guess the password decreases a lot!
linus at flowingcreativity dot net
8.07.2005 17:07
This may seem obvious, but I thought I would save someone from something that just confused me: you cannot use sleep() to sleep for fractions of a second. This:

<?php sleep(0.25) ?>

will not work as expected. The 0.25 is cast to an integer, so this is equivalent to sleep(0). To sleep for a quarter of a second, use:

<?php usleep(250000) ?>
5.07.2003 21:33
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.
hartmut at six dot de
25.08.2000 15:38
it is a bad idea to use sleep() for delayed output effects as

1) you have to flush() output before you sleep

2) depending on your setup flush() will not work all the way to the browser as the web server might apply buffering of its own or the browser might not render output it thinks not to be complete

netscape for example will only display complete lines and will not show table parts until the </table> tag arrived

so use sleep if you have to wait  for events and don't want to burn  to much cycles, but don't use it for silly delayed output effects!

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)