KodiTiger Posted December 15, 2010 Share Posted December 15, 2010 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 https://forums.phpfreaks.com/topic/221703-countdown-timer-on-page/ Share on other sites More sharing options...
.josh Posted December 15, 2010 Share Posted December 15, 2010 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 https://forums.phpfreaks.com/topic/221703-countdown-timer-on-page/#findComment-1147494 Share on other sites More sharing options...
KodiTiger Posted December 15, 2010 Author Share Posted December 15, 2010 Thanks very much , got a working timer now! Link to comment https://forums.phpfreaks.com/topic/221703-countdown-timer-on-page/#findComment-1147565 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.