php定时(持续时间)显示
所以我想在 .txt 文件中有一个数组,其中有单独的行,并拉出每一行并在一定时间后显示它...我不知道是否需要将 ajax 与 php 混合,或者这是否可以直接使用 php 。
foreach 是执行此操作的最佳方法吗?
<?php
$x=file("sayings.txt");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
我在哪里可以实现持续时间..需要等待吗?
像这样:
{ 回显$值。 “
”; 等待1000 //毫秒 使用javascript
来完成这个..如果你想要答案/代码,它就在这里: javascript 数组定时
So I want to have an array in a .txt file with separate lines and pull each line and display it after a certain duration... I don't know if I need to mix ajax with php or if this can just be straight php.
Is foreach the best way to do this?
<?php
$x=file("sayings.txt");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
Where can I implement a duration.. is there a wait?
like this:
{
echo $value . "
";
wait 1000 //miliseconds
}
Went with javascript for this one.. its here if you want the answer/code:
javascript array timed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
服务端解决方案
服务器端您可以这样做:
客户端解决方案
但是,您也可以使用带有参数(索引)的 Ajax 调用,该参数将在每次调用时返回下一个索引。您可以使用 Javascript 中的
SetTimeOut
来做到这一点。在 myServerPage.php 中,您只需检查 $_GET 即可检查索引并输出正确的行。 (验证数组索引是否正确)。另外,在服务器端,一旦你完成后发现数组不好,这可能会触发停止,你只需要向客户端返回数据(而不仅仅是行),然后告诉 JavaScript 停止。就是这样。希望对您有帮助。
Service side solution
Server side you could do this :
Client side solution
But, you can also use Ajax call with a parameter (an index) that will return the next index every call. You can do that with a
SetTimeOut
in Javascript.In myServerPage.php you just have to check the $_GET to check the index and to output the good line. (Validate if the array index is ok). Also, in the server side, once you have finish find that the array is not good this can be the trigger to stop and you just need to return a data to the client (instead of just the line) and will tell the javascript to stop. That's it. Hope it help you.
我假设您正在浏览器中运行它。
最好使用 javascript 在客户端执行此操作。您可以更好地控制客户端发生的情况。除非您的数据发生变化,否则您也不需要 ajax。
您可以在 PHP 端执行此操作,但可能会遇到缓冲问题(请参阅输出缓冲手册)。当您执行
sleep()
循环时,页面的其余部分也不会加载。I am assuming you are running this in a browser.
Its probably best to do it on client side using javascript. You have much more control of what happens on the client side. You wont need ajax either unless your data is changing.
You can kinda of do it on the PHP side, but you may run into buffering problems (Look at the manual for output buffering). Also the rest of the page wont load as you are going through your
sleep()
loop.您还可以查看 flush
通过此实现,savings.txt 中的更改执行不会更改输出,因为文件内容已被缓冲。
您还可以寻找 AJAX,它将重新获取文件并获取更新的内容。
You may also look at flush
With this implementation, a change in savings.txt during the execution won't change the output, as the file content is buffered.
You can also look for AJAX, wich will re-fetch the file and get and updated content.