1
$lastRequestTimestamp = microtime(true);
if (microtime(true) - $lastRequestTimestamp < 1) {
sleep(1);
}
$lastRequestTimestamp = microtime(true);
// now continue with the request ...
...的作品,但如果差異甚至幾毫秒,它睡覺1 second
這是重要的,考慮到我要做的請求的數量。睡覺的時差
我該如何讓腳本進入睡眠狀態,以便在請求之間始終有1秒的延遲,不會少於此時間。
所以'$ timeDifference = microtime(true) - $ lastRequestTimestamp'然後'usleep($ timeDifference)'? – 3zzy
@ 3zzy你不能使用'usleep',因爲它預期的微秒數,你的時差將以秒爲單位。 microtime以微秒精度返回以秒爲單位的時間。 – rtheunissen