执行之间的延迟-PHP
是否可以在PHP中的两部分代码之间设置延迟?我想要这样的东西-执行之间的延迟-PHP,php,Php,是否可以在PHP中的两部分代码之间设置延迟?我想要这样的东西- <?php function firstFunc(){ echo "anything"; } function secondFunc(){ echo "something"; } // call first fumction firstFunc(); // I want the script to wait for 2 seconds here, then call second function seco
<?php
function firstFunc(){
echo "anything";
}
function secondFunc(){
echo "something";
}
// call first fumction
firstFunc();
// I want the script to wait for 2 seconds here, then call second function
secondFunc();
您应该使用函数如果您想延迟某项操作的执行,可以使用睡眠($seconds)
函数。以下是文档中的内容
将程序执行延迟给定的秒数
成功时返回0,错误时返回false
因此,您可以执行以下操作:
<?php
function firstFunc(){
echo "anything";
}
function secondFunc(){
echo "something";
}
// call first function
firstFunc();
sleep(2); //Delays execution for 2 seconds.
secondFunc();
?>
请记住,PHP是一种服务器端语言。客户端将从PHP脚本(如果客户端必须接收任何内容)接收的唯一内容是HTML(或CSS、JS等)。因此,当您运行此代码时,页面将在2秒钟内不显示任何内容,然后一起输出anything
和something
,因为延迟发生在服务器级别,而不是客户端机器上。如果您想要一种延迟,在屏幕上可以看到某物
,然后在2秒后出现任何东西
,您需要使用JavaScript
希望这有帮助:)RTFM: