How To Use setInterval in PHP?

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);

your solution is quite simple :

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

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


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

So it would look something like this using 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>

Tags:

Php