• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

php function after certain delay

i want to call my function php function after some delat say after 20 seconds. how can i do that?

or if i want to call a php function after every 20 seconds
0
nandananushil1
Asked:
nandananushil1
  • 7
  • 5
  • 3
  • +7
1 Solution
 
Jerry MillerCommented:
You can use the sleep() function.

http://php.net/manual/en/function.sleep.php
0
 
ondrejsimekCommented:
To call it every 20 seconds, use e.g. this:
while (!sleep(20))
    yourFunction();

Open in new window

0
 
psreedhar2000Commented:
whether you are running the program through a browser or internally on the server. Some more details may be required if the above solutions do not work.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nandananushil1Author Commented:
sleep is deplaying execution of whole page,

<p>hello</p>
<?php
sleep(10);
?>
<p>hi</p>
hello is printed after 10 seconds, i want hello to print then after 10 seconds it should print hi. i know how to do it with javascript, but is it possible to do it with php?
0
 
ondrejsimekCommented:
Try flush ( http://cz.php.net/manual/en/function.flush.php )
<p>hello</p>
<?php
sleep(10);
flush()l
?>
<p>hi</p>

Open in new window

0
 
ondrejsimekCommented:
Sorry for the typo - once again the code..
<p>hello</p>
<?php
sleep(10);
flush();
?>
<p>hi</p>

Open in new window

0
 
nandananushil1Author Commented:
@ondrejsimek: it is still giving whole output after 10 seconds.

http://learnsmartways.com/a1.php
0
 
ondrejsimekCommented:
Try to add ob_flush() ...
<p>hello</p>
<?php
sleep(10);
flush(); ob_flush();
?>
<p>hi</p>

Open in new window

0
 
nandananushil1Author Commented:
@ondrejsimek:

i am getting same examples as u provided everywhere on net, i am running wamp on windows 7. can that be a problem? or is there any setting which i have to change in php.ini to make it work?
0
 
ondrejsimekCommented:
Oh, sorry, it should be like this..
<p>hello</p>
<?php
flush(); ob_flush();
sleep(10);
?>
<p>hi</p>

Open in new window

0
 
nandananushil1Author Commented:
still same thing :(
0
 
trencH87Commented:
No need for ob_flush(). The code below (As ondrejsimek first stated) should work.
<p>hello</p>
<?php
flush();
sleep(10);
?>
<p>hi</p>

Open in new window

0
 
ondrejsimekCommented:
A compression may prevent that work.. Try the following code.
<?php

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
echo '<p>hello</p>';
flush();
sleep(10);
echo '<p>hi</p>';

Open in new window

0
 
trencH87Commented:
The problem rises when you put this on your site. Javascript is more suited for this task. You can execute php within javascript you know :o)
0
 
nandananushil1Author Commented:
yes :) trencH87 but i was considering the case if javascript is turned off thats why i was more focused on php
0
 
trencH87Commented:
Users turning off javascript are just interested in getting plain information. They don't want these fansy features provided with javascript. Neither do they want to wait for your fansy php-script to execute. Just give them what they want: Text.
0
 
ad4m1Commented:
I personally agree with trench87. You can use:
window.setTimeout("Function Name",20000);
to do what you want. The function can be a call to another javascript function or you can do an AJAX call which would then fire the execution back to a php script on your server.
With regards to people turning off javascript, I don;t know many that do these days. Back about 6 years ago javascript was heavily used by dodgy websites to fire up pop'ups or drop tracking cookies etc but these days most modern browsers have got round these issues so Javascript is much more safe. Plus with many sites using javascript like google, bing, bbc etc, many people have decided that turning off javascript is more detrimental that it is benifical. Also Facebook won;'t even begin to work without javascript running as the whole site relies entirely on Ajax calls.
The the above said I would seriously consider using the setTimeout javascript function to do what your after. If you need the Ajax call code to execute the function back at your server then here it is.
<script language="javascript">
function CallServerScript()
   {
   if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp=new XMLHttpRequest();
     }
   else
     {// code for IE6, IE5
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
   xmlhttp.onreadystatechange=function()
     {
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
        // HERE GOES WHAT YOU WANT TO DO WITH YOUR OUTPUT FROM YOUR PHP SCRIPT
        // SIMPLY USE xmlhttp.responseText TO GET THE CONTENTS OF YOUR SCRIPT AND USE JAVASCRIPT LIKE BELOW TO UTILISE IT.
       document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
       }
     }
   xmlhttp.open("GET","myPHPServerScript.php",true);
   xmlhttp.send();
   }
</script>
Hope this helps. Let me know if you want to go with the Ajax call and I can help you out with the calls back at your webpage side to format your php scripts output onto your web page.
0
 
johnwardeCommented:
The example below uses the meta data tag to force the browser to refresh the same page every 10 seconds.  The key line is ...



This works with javascript turned off.

John

<?php

function getStatus() {
    return date('d/m/Y h:i:sa');
}
$strStatus = getStatus();

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Refresh Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="refresh" content="10">
  </head>
  <body>
    Hello, time is <?php echo $strStatus; ?>
  </body>
</html>

Open in new window

0
 
johnwardeCommented:
Also, optionally you can specify a URL to refresh too ...



e.g.

John
0
 
Ray PaseurCommented:
Does the function need to be executed synchronously?  In other words, do you need the function results in your script?  If you do, there are some important considerations and for the most part, your script will, in effect, "wait" because even if you use flush() it does not guarantee that the browser will actually display the output at the exact moment of flush().

However if you do not need the results in your script, you may be able to move the function into an API call to a web service.  The web service script will begin with sleep(20) and will then execute the function.  To call the web service, you will want to use CURL because file_get_contents() and similar constructs will be synchronous and will not complete till the service closes the connection.

Good luck with it, ~Ray
0
 
Vimal DMCommented:
Hai,

>> Why don't you study on 'PHP CRON JOBS" where can make the functions and set it in the cron jobs with time duration,which will call the function according to the timing that you have mentioned.

Thanks
0
 
ondrejsimekCommented:
vimalmaria: I don't think that cron allows durations less than a minute. I usually make daemons for things like this one.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 5
  • 3
  • +7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now