如何使用 SLEEP() 函数在 PHP 中暂停脚本几分之一秒?
sleep(1); #waits/sleeps for one second then continue running the script
Q1.如何将其变为 1/100 秒?其中哪个有效: 0,01
或 0.01
或 .01
?
Q2。什么是替代方案? wait();
或 snap();
??它们有何不同(更精确/更不精确)?
sleep(1); #waits/sleeps for one second then continue running the script
Q1. How to make this 1/100 of a second? which of these work: 0,01
or 0.01
or .01
?
Q2. What are alternatives? wait();
or snap();
?? how do they differ (more/less precise)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以上都不是!
usleep
是您想要的分数一秒钟的时间。usleep(100000)
将休眠十分之一秒。您的其他选项是
time_nanosleep
这需要几秒和纳秒(其中十亿是一秒),并且time_sleep_until
,它将休眠直到达到特定的 unix 时间戳。请注意,您的系统可能没有毫秒分辨率,甚至纳秒分辨率。您可能会在极短的时间内难以入睡。
None of the above!
usleep
is what you want for fractions of a second.usleep(100000)
will sleep for one tenth of one second.Your other options are
time_nanosleep
which takes both seconds and freaking nanoseconds (one billion of which are one second), andtime_sleep_until
, which will sleep until a particular unix timestamp has been reached.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.
迟到的答案...但您可以使用 time_nanosleep(),即:
休眠
1/10
秒 (0.1
):休眠
1/100
秒 (0.01
) code>):睡眠
1/1000
秒 (0.001
):Late answer... but you can use time_nanosleep(), i.e:
To sleep for
1/10
of a second (0.1
):To sleep for
1/100
of a second (0.01
):To sleep for
1/1000
of a second (0.001
):使用 usleep ,您可以在其中以微秒为单位传递,
因此对于您的情况,您可以致电
usleep(10000)
睡眠 1/100 秒。Use usleep in which you can pass in microseconds,
so for your case you can call
usleep(10000)
to sleep for 1/100 of a second.您正在寻找的是
usleep()
或time_nanosleep()
。至于你的第二个问题,所有这些方法都具有很高的精度,但是我建议你在你的特定系统上进行测试(如果它很重要)。
What you are looking for is
usleep()
ortime_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.