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.
sleep
(PHP 4, PHP 5)
sleep — 実行を遅延させる
説明
int sleep
( int $seconds
)
seconds で与えられた秒数ぶんプログラムの実行を遅延させます。
パラメータ
- seconds
-
秒単位の停止時間。
返り値
成功した場合にゼロ、エラー時に FALSE を返します。 シグナルによって中断された場合は、sleep() は残りの秒数を返します。
エラー / 例外
指定した秒数 seconds が負の場合、 この関数は E_WARNING を発生させます。
例
例1 sleep() の例
<?php
// 現在の時刻
echo date('h:i:s') . "\n";
// 10 秒間遅延させる
sleep(10);
// 再開!
echo date('h:i:s') . "\n";
?>
この例の(10 秒後の)出力は以下のようになります。
05:31:23 05:31:33
参考
- usleep() - マイクロ秒単位で実行を遅延する
- time_nanosleep() - 秒およびナノ秒単位で実行を遅延する
- time_sleep_until() - 指定した時刻まで実行を遅延する
- set_time_limit() - 実行時間の最大値を制限する
sleep
f dot schima at ccgmbh dot de
10-Nov-2009 02:48
10-Nov-2009 02:48
Anonymous
08-Feb-2009 08:32
08-Feb-2009 08:32
This will allow you to use negative values or valuer below 1 second.
<?php slaap(0.5); ?>
<?php
function slaap($seconds)
{
$seconds = abs($seconds);
if ($seconds < 1):
usleep($seconds*1000000);
else:
sleep($seconds);
endif;
}
?>
webseos at gmail dot com
27-Aug-2008 04:29
27-Aug-2008 04: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 :
<?php
ob_implicit_flush(true);
for($i=0;$i<5;$i++)
{
$dis=<<<DIS
<div style="width:200px; background-color:lime;border:1px; text-align:center;text-decoration:blink;">
$i
</div>
DIS;
echo $dis;
sleep(5);
//flush();
}
marpetr at gmail dot com
18-Mar-2008 07:41
18-Mar-2008 07: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
08-Jul-2005 03:07
08-Jul-2005 03: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) ?>
MPHH
05-Jul-2003 07:33
05-Jul-2003 07: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-Aug-2000 01:38
25-Aug-2000 01: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!
