简体   繁体   English

如何使用SLEEP()函数在PHP中暂停一小段时间的脚本?

[英]How to pause a script just for a fraction of a second in PHP using the SLEEP() function?

sleep(1);   #waits/sleeps for one second then continue running the script

Q1. Q1。 How to make this 1/100 of a second? 怎么做1/100秒? which of these work: 0,01 or 0.01 or .01 ? 哪些工作: 0,010.01.01

Q2. Q2。 What are alternatives? 什么是替代品? wait(); or snap(); snap(); ?? ?? how do they differ (more/less precise)? 它们有何不同(更多/更不精确)?

Q1. Q1。 How to make this 1/100 of a second? 怎么做1/100秒? which of these work: 0,01 or 0.01 or .01 ? 哪些工作:0,01或0.01或.01?

None of the above! 以上都不是!

usleep is what you want for fractions of a second. usleep是你想要的几分之一秒。 usleep(100000) will sleep for one tenth of one second. usleep(100000)将睡眠十分之一秒。

Your other options are time_nanosleep which takes both seconds and freaking nanoseconds (one billion of which are one second), and time_sleep_until , which will sleep until a particular unix timestamp has been reached. 您的其他选项是time_nanosleep这需要两秒钟, 再用纳秒 (其中一个十亿是一秒),以及time_sleep_until ,这将睡眠状态,直到一个特定的UNIX时间戳已经达到。

Be aware that your system might not have millisecond resolution, no less nanosecond resolution. 请注意,您的系统可能没有毫秒级的分辨率,也不会有毫微秒的分辨率。 You might have trouble sleeping for precisely tiny, tiny amounts of time. 您可能无法在很短的时间内睡觉。

Use usleep in which you can pass in microseconds, 使用usleep ,你可以通过微秒,

so for your case you can call usleep(10000) to sleep for 1/100 of a second. 所以对于你的情况,你可以叫usleep(10000)睡觉1/100秒。

Late answer... but you can use time_nanosleep() , ie: 迟到的答案......但你可以使用time_nanosleep() ,即:

To sleep for 1/10 of a second ( 0.1 ): 睡眠1/10秒( 0.1 ):

time_nanosleep(0, 100000000);

To sleep for 1/100 of a second ( 0.01 ): 睡眠1/100秒( 0.01 ):

time_nanosleep(0, 10000000);

To sleep for 1/1000 of a second ( 0.001 ): 睡眠1/1000秒( 0.001 ):

time_nanosleep(0, 1000000);

What you are looking for is usleep() or time_nanosleep() . 你要找的是usleep()time_nanosleep()

As for your second question, all these methods come with a high level of precision however I would advise you to test on your specific system if it's critical. 至于你的第二个问题,所有这些方法都具有高精度,但我建议你测试你的特定系统是否很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM