Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making an egg timer with sleep and running into timeout problems. 1

Status
Not open for further replies.

PockyBum522

Programmer
Jun 10, 2003
239
US
This code should count seconds up and show you on a new line every second. What it does is nothing for 30 seconds, then after it times out it shows me 00:00 through 00:29. How do I have it show me it counting realtime? I've run into this problem on visual basic solved by the doevents command. Any ideas?

-----------PHP START----------
$Seconds = 0;
$Minutes = 0;

while (1>.5){
echo ($Minutes.":".$Seconds."<br>");
$Seconds = $Seconds + 1;

if ($Seconds >= 60) {$Seconds = 0; $Minutes ++;}
sleep (1);
}
------------PHP END----------
 
Your script's behaviour makes perfect sense, it is just that you do not understand how internet applications work. Opposite to desktop applications, they are discontinuous, meaning that your browser requests a page, scripting language executes on the server and serves the page and connection is lost. When you request another page connection is restored and the whole thing happens all over again. Now to see what your script is doing:

Your while look will always evaluate as true, but I guess that was you point. Then the script starts producing output but waits one second to generate each line. Since page can only be served when php has done all the work, your page is not served. PHP by default has a timeout of 30 seconds. If your script has not ended by that it is terminated. And that is what happens with you. After thirty seconds php script gets terminated and outputs as much as it has done by then. And that is counter from 0 to 29 seconds.
 
Alright, I assumed there were better languages to write this in, javascript looking like the best. I just wanted to try it. Is there any way I can make it do what I want to do? (Show the user an updated clock every second)
 
you could refresh the page, but its clunky inefficient and generally pants.

use java, it shows a certain competencey++

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Not sure, what does the ? operator do :D

ok, php related:

str_replace("competencey","competency",$_TEK_TIPS['My_other_post']).

print("Slei 1, Karv 0");



______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
You can make a script run forever:

set_time_limit(0);

That way it will run until the browsers stop button is pressed...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top