How To Use setInterval in PHP?-ThrowExceptions

Exception or error:

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.

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

<?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.

How to solve:

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

Answer´╝Ü

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>

Answer´╝Ü

your solution is quite simple :

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

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

Answer´╝Ü

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.

Answer´╝Ü

Javascript executes setInterval in 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.

Answer´╝Ü

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

Answer´╝Ü

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

Are you aware that PHP only delivers content on request?

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *