如何在 PHP 中使用 setInterval?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12783737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:13:45  来源:igfitidea点击:

How To Use setInterval in PHP?

php

提问by Heart

I want to ask that how can i use php function again and again after some time automatically just like setInterval in Javascript. We set the time and it is on its job until the script is aborted or closed.

我想问一下,我怎样才能像 Javascript 中的 setInterval 一样在一段时间后自动一次又一次地使用 php 函数。我们设置了时间,它一直在工作,直到脚本中止或关闭。

INDEX.PHP

索引文件

<?
$Wall = new Wall_Updates();
$updatesarray=$Wall->Updates();
foreach($updatesarray as $data)
{
    $likes=$data['likes'];
    ?>
    <div id="likes"><?php echo $likes; ?></div>
    <?
}
?>

And Wall_Updates() Function is defined here in FUNCTION.PHP

而 Wall_Updates() 函数在FUNCTION.PHP 中定义在这里

<?php

class Wall_Updates {    
    public function Updates() {
        $myname=$_COOKIE['usename'];
        $query=mysql_query("SELECT * FROM posts WHERE name='$myname'");
        while($row=mysql_fetch_array($query))
            $data[]=$row;
        return $data;
    }
}
?>

I want this Wall_Updates() function to fetch data from mysql again and again.So, It will be Updated.

我希望这个 Wall_Updates() 函数一次又一次地从 mysql 获取数据。所以,它将被更新。

回答by Ruslan Polutsygan

For the record: I think it's bad idea. But whatever :)

为了记录:我认为这是个坏主意。但是无所谓 :)

Try this code

试试这个代码

function setInterval($f, $milliseconds)
{
    $seconds=(int)$milliseconds/1000;
    while(true)
    {
        $f();
        sleep($seconds);
    }
}

Usage:

用法:

setInterval(function(){
    echo "hi!\n";
}, 1000);

Or:

或者:

$a=1; 
$b=2;

setInterval(function() use($a, $b) {
    echo 'a='.$a.'; $b='.$b."\n";
}, 1000);

回答by Sol

I would suggest using setInterval to poll for results from a .php page using AJAx and then output your results.

我建议使用 setInterval 从使用 AJAx 的 .php 页面轮询结果,然后输出结果。

So it would look something like this using jQuery:

所以它看起来像这样使用 jQuery:

<script>
    var poll = true;
    var getData = function() {
        if (poll) {
            $.get('getData.php', function(data) { $('#likes').html(data); });
        }
    };

    $(document).ready(function() {
        setInterval(getData, 5000);
        $('.comm').click(function() { poll = false; });
        $('.comm').blur(function() { poll = true; });
    });
</script>

回答by eladcm

your solution is quite simple :

您的解决方案非常简单:

   $event = new EVTimer(10,2, function() {
         Do your things .. :)
   });

https://www.php.net/manual/en/ev.examples.php

https://www.php.net/manual/en/ev.examples.php

回答by ugwu jude

I will surgest that you use a cron job to implement such functionality in your code. Cron will run in the background based on the instruction you give it. check out this article

我强烈建议您使用 cron 作业在您的代码中实现此类功能。Cron 将根据您给它的指令在后台运行。看看这篇文章

回答by Amir Fo

Javascript executes setIntervalin other threads to continue its code-flow execution. But php hangs on the line you have called the function that is implemented by for loops. However php supports multithreading and forking tools, but they're not recommended and not thread safety yet.

JavascriptsetInterval在其他线程中执行以继续其代码流执行。但是 php 挂在你调用由 for 循环实现的函数的行上。然而 php 支持多线程和分叉工具,但不推荐使用它们,而且还不是线程安全的。

回答by Martin Müller

Unlike Javascript, PHP is executed on the server side. There is no setTimeout functionality in PHP. You can get close by using cronjobs - or other PHP scripts - that call your scripts though.

与 Javascript 不同,PHP 在服务器端执行。PHP 中没有 setTimeout 功能。您可以通过使用调用您的脚本的 cronjobs(或其他 PHP 脚本)来接近。

回答by Erwin Moller

It isn't clear what you want to achieve exactly.

目前尚不清楚您想要达到的确切目标。

Are you aware that PHP only delivers content on request?

您是否知道 PHP 仅根据请求提供内容?

If you want the server to update something once in a while (a file for example), use a cronjob (on *nix).

如果您希望服务器不时更新某些内容(例如文件),请使用 cronjob(在 *nix 上)。

If you want your WEBPAGE to re-query something, do it in javascript and call a PHP script that delivers fresh content.

如果您希望您的 WEBPAGE 重新查询某些内容,请在 javascript 中执行并调用提供新鲜内容的 PHP 脚本。