(PHP 5, PHP 7, PHP 8)
time_nanosleep — 延緩執(zhí)行若干秒和納秒
程序延緩執(zhí)行指定數(shù)量的 seconds
和 nanoseconds
。
seconds
必須是一個(gè)非負(fù)整數(shù)。
nanoseconds
必須是一個(gè)小于1億的非負(fù)整數(shù)。
成功時(shí)返回 true
, 或者在失敗時(shí)返回 false
。
如果延遲被中斷,一個(gè)關(guān)聯(lián)數(shù)組會(huì)返回,內(nèi)容為:
seconds
- 延遲剩余未執(zhí)行的秒數(shù)
nanoseconds
- 延遲剩余未執(zhí)行的納秒數(shù)
版本 | 說明 |
---|---|
5.3.0 | 至此之后該函數(shù)在Windows平臺(tái)下可用。 |
示例 #1 time_nanosleep() 的例子
<?php
// Careful! This won't work as expected if an array is returned
if (time_nanosleep(0, 500000000)) {
echo "Slept for half a second.\n";
}
// This is better:
if (time_nanosleep(0, 500000000) === true) {
echo "Slept for half a second.\n";
}
// And this is the best:
$nano = time_nanosleep(2, 100000);
if ($nano === true) {
echo "Slept for 2 seconds, 100 microseconds.\n";
} elseif ($nano === false) {
echo "Sleeping failed.\n";
} elseif (is_array($nano)) {
$seconds = $nano['seconds'];
$nanoseconds = $nano['nanoseconds'];
echo "Interrupted by a signal.\n";
echo "Time remaining: $seconds seconds, $nanoseconds nanoseconds.";
}
?>