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
JavaScript
x
<?
$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
JavaScript
<?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.
Advertisement
Answer
For the record: I think it’s bad idea. But whatever 🙂
Try this code
JavaScript
function setInterval($f, $milliseconds)
{
$seconds=(int)$milliseconds/1000;
while(true)
{
$f();
sleep($seconds);
}
}
Usage:
JavaScript
setInterval(function(){
echo "hi!n";
}, 1000);
Or:
JavaScript
$a=1;
$b=2;
setInterval(function() use($a, $b) {
echo 'a='.$a.'; $b='.$b."n";
}, 1000);