Jump to content

Countdown Timer on Page


KodiTiger

Recommended Posts

Newbie PHP user here, so go easy on me :) .

 

I've been trying to make a timer that starts on an arbitrary number of seconds, say 10 for example, then counts down to 0 (and refreshes itself every second for the user to see how much time is remaining), and then some more code is executed when 0 is reached.

 

I attempted to do something like:

 

<?php

 

$goal = time() + 10;

 

while($goal - time() >= 0) {

 

echo "$goal - time()";

sleep(1);

 

}

 

?>

 

This didn't seem to work as my page just took a while to load - I suspect it was going through the entire while loop before displaying anything rather than just dynamically updating the timer every second. Also when I chose 100 instead of 10, the webpage didn't seem to even finish loading. Also it displayed a lot of odd text - not a timer at all!

 

Help would be much appreciated :) !

Link to comment
Share on other sites

php is a server-side language.  When the script is requested (like when you go to http://www.yoursite.com/yourscript.php) the server parses and executes all php and then outputs the results (if any) as the response (which the browser/client does whatever with it).  You can *technically achieve what you are doing using ob_flush but it is seriously not advisable. 

 

If you want to have a countdown timer like that, you should use javascript, which is a client-side language. Basically you would just use setTimeout() to countdown (lots of basic js tuts for timers, give it a go in google).

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.