php定时(持续时间)显示

发布于 2024-11-25 21:18:44 字数 509 浏览 2 评论 0原文

所以我想在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的海 2024-12-02 21:18:44

服务端解决方案

服务器端您可以这样做:

<?php

$x=file("sayings.txt");
foreach ($x as $value)
{
    echo $value . "<br />";//Send the output
    flush();               //Make sure it's outputted
    sleep(1);              //Sleep 1 second
}
?> 

客户端解决方案

但是,您也可以使用带有参数(索引)的 Ajax 调用,该参数将在每次调用时返回下一个索引。您可以使用 Javascript 中的 SetTimeOut 来做到这一点。

<script type="text/javascript">
var arrayIndex = 0;
function getArrayData()
{
    var dataFromServer="";
    $.get("myServerPage.php", { 'arrayIndex': arrayIndex },
         function(data){
         alert("Line : " + data);
         arrayIndex++;
         var t=setTimeout("getArrayData()",1000);//Every 1 second
     });
   
}
</script>

在 myServerPage.php 中,您只需检查 $_GET 即可检查索引并输出正确的行。 (验证数组索引是否正确)。另外,在服务器端,一旦你完成后发现数组不好,这可能会触发停止,你只需要向客户端返回数据(而不仅仅是行),然后告诉 JavaScript 停止。就是这样。希望对您有帮助。

Service side solution

Server side you could do this :

<?php

$x=file("sayings.txt");
foreach ($x as $value)
{
    echo $value . "<br />";//Send the output
    flush();               //Make sure it's outputted
    sleep(1);              //Sleep 1 second
}
?> 

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.

<script type="text/javascript">
var arrayIndex = 0;
function getArrayData()
{
    var dataFromServer="";
    $.get("myServerPage.php", { 'arrayIndex': arrayIndex },
         function(data){
         alert("Line : " + data);
         arrayIndex++;
         var t=setTimeout("getArrayData()",1000);//Every 1 second
     });
   
}
</script>

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.

乙白 2024-12-02 21:18:44

我假设您正在浏览器中运行它。

最好使用 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.

薄情伤 2024-12-02 21:18:44
<?php

    $x=file("sayings.txt");
    foreach ($x as $value)
      {
      echo $value . "<br />";
      sleep(1); //wait 1000ms
      echo str_repeat(" ", 50000); //Send output buffer
      flush();
      }
    ?> 
<?php

    $x=file("sayings.txt");
    foreach ($x as $value)
      {
      echo $value . "<br />";
      sleep(1); //wait 1000ms
      echo str_repeat(" ", 50000); //Send output buffer
      flush();
      }
    ?> 
喵星人汪星人 2024-12-02 21:18:44

您还可以查看 flush

<?php

$x=file("sayings.txt");
foreach ($x as $value)
{
    echo $value . "<br />";
    flush();   // send the content to the browser
    sleep(1);  // sleep one second
}
?> 

通过此实现,savings.txt 中的更改执行不会更改输出,因为文件内容已被缓冲。

您还可以寻找 AJAX,它将重新获取文件并获取更新的内容。

You may also look at flush

<?php

$x=file("sayings.txt");
foreach ($x as $value)
{
    echo $value . "<br />";
    flush();   // send the content to the browser
    sleep(1);  // sleep one second
}
?> 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文