v0idmp3 Posted June 17, 2012 Share Posted June 17, 2012 Hi I have a little problem. I develop browser game and I dont know how to build countdown timer. For example...When player click on "Build Something", I need to show him a countdown timer, from start time to some time in future, and countdown difference from start and end time. And than, when timer reach zero, I need to put some data in database. I think a lot about that, and I realized that I must have in my database start and end time, but I dont know how to calculate remaing time when player logout from game, and than log in again. How to show him how much time was spend when he was offline? If you know some tutorial about Ajax/JS countdown timers, I will be very thankful. I searched google, but I didnt find what I need. I'm new to ajax/js and I really dont know how to make that. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/ Share on other sites More sharing options...
smoseley Posted June 17, 2012 Share Posted June 17, 2012 var timeLeft = 300; // 5 minutes var timer = window.setInterval(function() { timeLeft--; var minutesLeft = Math.floor(timeLeft / 60); var secondsLeft = timeLeft % 60; console.log('Time left: ' + minutesLeft + ':' + secondsLeft); if (timeLeft == 0) { window.clearInterval(timer); // do some ajax thing } }, 1000); Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354589 Share on other sites More sharing options...
v0idmp3 Posted June 17, 2012 Author Share Posted June 17, 2012 Ok, thanks for code...But I need to pass variable "timeLeft" from PHP. How to do that? Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354597 Share on other sites More sharing options...
smoseley Posted June 17, 2012 Share Posted June 17, 2012 If my snippet didn't fully answer your "little problem" (and then some), I'm afraid you don't have enough knowledge to build the game you want to make. I suggest you buy books on PHP & Javascript and do some reading. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354637 Share on other sites More sharing options...
smoseley Posted June 17, 2012 Share Posted June 17, 2012 By the way here's the answer to your last question: <?php // Some code $timeLeft = 300; ?> <script type="text/javascript"> var timeLeft = <?=$timeLeft?>; // 5 minutes var timer = window.setInterval(function() { timeLeft--; var minutesLeft = Math.floor(timeLeft / 60); var secondsLeft = timeLeft % 60; console.log('Time left: ' + minutesLeft + ':' + secondsLeft); if (timeLeft == 0) { window.clearInterval(timer); // do some ajax thing } }, 1000); </script> But of course, your next question will be... Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354638 Share on other sites More sharing options...
v0idmp3 Posted June 17, 2012 Author Share Posted June 17, 2012 Hm...When I try to use your code, it's doesn't work? I simply have white page, and nothing on it. Can you recommend me some book about PHP and JS? Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354667 Share on other sites More sharing options...
smoseley Posted June 18, 2012 Share Posted June 18, 2012 I can't recommend a book. Look on Amazon for highly rated books. My code is logging to the console. If you hit F12 in a console-enabled browser, and click the console tab, you'll see the output. Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354753 Share on other sites More sharing options...
Mahngiel Posted June 18, 2012 Share Posted June 18, 2012 Likely his .ini isn't set to use short-tags. He'll need to either enable that, or use var timeleft = <?php echo$timeleft; ?>; Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354817 Share on other sites More sharing options...
smoseley Posted June 18, 2012 Share Posted June 18, 2012 Likely his .ini isn't set to use short-tags. He'll need to either enable that, or use var timeleft = <?php echo $timeleft; ?>; Likely not That's very rare. Likely the issue is he just doesn't understand how JS console works. Quote Link to comment https://forums.phpfreaks.com/topic/264327-countdown-timer-ajax-and-php/#findComment-1354906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.