Solved

php function after certain delay

Posted on 2010-09-18
23
383 Views
Last Modified: 2012-08-13
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
Comment
Question by:nandananushil1
  • 7
  • 5
  • 3
  • +7
23 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 33707231
You can use the sleep() function.

http://php.net/manual/en/function.sleep.php
0
 
LVL 2

Expert Comment

by:ondrejsimek
ID: 33707303
To call it every 20 seconds, use e.g. this:
while (!sleep(20))
    yourFunction();

Open in new window

0
 

Expert Comment

by:psreedhar2000
ID: 33707341
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
 
LVL 1

Expert Comment

by:edup
ID: 33707374
0
 

Author Comment

by:nandananushil1
ID: 33707378
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
 
LVL 2

Expert Comment

by:ondrejsimek
ID: 33707383
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
 
LVL 2

Expert Comment

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

Open in new window

0
 

Author Comment

by:nandananushil1
ID: 33707400
@ondrejsimek: it is still giving whole output after 10 seconds.

http://learnsmartways.com/a1.php
0
 
LVL 2

Expert Comment

by:ondrejsimek
ID: 33707407
Try to add ob_flush() ...
<p>hello</p>
<?php
sleep(10);
flush(); ob_flush();
?>
<p>hi</p>

Open in new window

0
 

Author Comment

by:nandananushil1
ID: 33707410
@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
 
LVL 2

Expert Comment

by:ondrejsimek
ID: 33707412
Oh, sorry, it should be like this..
<p>hello</p>
<?php
flush(); ob_flush();
sleep(10);
?>
<p>hi</p>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:nandananushil1
ID: 33707427
still same thing :(
0
 
LVL 4

Expert Comment

by:trencH87
ID: 33707430
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
 
LVL 2

Expert Comment

by:ondrejsimek
ID: 33707441
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
 
LVL 4

Expert Comment

by:trencH87
ID: 33707442
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
 

Author Comment

by:nandananushil1
ID: 33707443
yes :) trencH87 but i was considering the case if javascript is turned off thats why i was more focused on php
0
 
LVL 4

Expert Comment

by:trencH87
ID: 33707447
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
 
LVL 3

Accepted Solution

by:
ad4m1 earned 500 total points
ID: 33707571
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
 
LVL 2

Expert Comment

by:johnwarde
ID: 33707590
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
 
LVL 2

Expert Comment

by:johnwarde
ID: 33707610
Also, optionally you can specify a URL to refresh too ...



e.g.

John
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33707653
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
 
LVL 7

Expert Comment

by:Vimal DM
ID: 33708152
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
 
LVL 2

Expert Comment

by:ondrejsimek
ID: 33708211
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now