jeremyhowell Posted December 2, 2009 Share Posted December 2, 2009 I am building a php Facebook app, sort of like Mafia Wars, and I need a script for a countdown timer till the next energy/health refill. I am using a timestamp at the moment, but I want to display a timer when the user is viewing the page that shows how long till all the regenerable fields are topped up, ie stamina, energy, health. For example, I want the server to add 1 value to a database entry, if it hasn't reached the max value yet, every 2 minutes or so, even when the user is not viewing the page, and I want the user to see it happening when they are viewing their profile page. See this image if you do not completely understand: http://www.freeimagehosting.net/image.php?8f0163db6b.jpg Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/ Share on other sites More sharing options...
blueman378 Posted December 2, 2009 Share Posted December 2, 2009 the following is assuming you do not have access to a cron job system. the following is assuming you update every 5 minutes (30 0000 milliseconds). the following assumes you add 5 energy when you update. right, well the simplest way of doing it that i can see is to have one db field, pupdate -> this field holds the time of the previous update, a timestamp will do. while(currenttime - pupdate > 30 0000 ) pupdate += 30 0000 foreach(user) energy += 5 endforeach endwhile (because of the previous loop we know there will be less than 5 minutes before the next update) timeremaining = currenttime - pupdate / 100 this gives us the time remaining in seconds minutes = timeremaining / 60 seconds = timeremaining % 60 so now you have two variables you can feed into your javascript timer the minutes and seconds or if it only takes it in seconds simply removew the last two steps and give it timeremaining directly. the rest is javascript which im not exactly strong in, so good luck. This algorithm has not been tested although i believe it should work Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-969492 Share on other sites More sharing options...
mikesta707 Posted December 2, 2009 Share Posted December 2, 2009 If you want a system like Mafia Wars has, you will have to look into AJAX. clicky Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-969501 Share on other sites More sharing options...
jeremyhowell Posted December 2, 2009 Author Share Posted December 2, 2009 Thank you, Blueman, I think I can work with that. I myself am not that strong in Javascript either, I got a book thicker than an encyclopedia on it (no seriously, its about the size of 3 good sized King James Bibles stuck together), but I havent got past the first chapter yet. Can anyone help with the code for a Javascript timer? or maybe something using AJAX that sends the new timestamp every 5 minutes to the database when the counter hits 5 minutes, then resets it? I dont think Ill worry about doing a loop for all users at the moment, I'll start with doing it for one user. Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-970150 Share on other sites More sharing options...
jeremyhowell Posted December 3, 2009 Author Share Posted December 3, 2009 Ok, I feel like I conquered the world, I have written a Javascript that displays a timer, but I need help implementing it to the PHP script to make it update the timestamp every 5 minutes? Maybe an AJAX guru could help? <script> <!-- // var milisec=0 var seconds=1000 document.counter.d2.value='1000' function display(){ if (milisec<=0){ milisec=9 seconds-=1 } if (seconds<=-1){ milisec=0 seconds+=1 } else milisec-=1 if (seconds>=60) { var minutes = Math.round((seconds/60)); } document.counter.d2.value=minutes+":"+(seconds%60) setTimeout("display()",100) } display() --> </script> ~ Jeremy Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-970197 Share on other sites More sharing options...
blueman378 Posted December 3, 2009 Share Posted December 3, 2009 nice work on the countdown timer, now ajax isnt really needed for this, and in turn you could run into problems with it. this is because if you use ajax then every user that is on will be sending a request to add energy. what i mentioned above will work. all you need to do to your javascript is ensure that when it gets to 0.00 it resets to 5.00 and use javascript again to change the text inside the div, grab the number that is already there, and then add 5 to it. so your best bet <div id="energyinfo"> <p>you have <span id=energyamount">15</span> energy!</p> </div> now your javascript must function timeempty(){ var energyamount = document.getElementById ('energyamount).innerhtml; energyamount += 5; document.getElementById ('energyamount).innerhtml = energyamount; seconds=1000; } or something similar. what is happening on the client side is only visual, eg if i was to find out your javascript functions and call them through the browser window it will have no effect on the actual server. Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-970316 Share on other sites More sharing options...
jeremyhowell Posted December 3, 2009 Author Share Posted December 3, 2009 Thanks for the reply, blueman. I am sort of at a loss at how to update the database when the timer hit 5 seconds? Can I add a PHP script in a Javascript function, maybe like this: <script language="javascript> function func() { <?php echo "hello" world"; ?> } </script> and when the timer hits 5 seconds, the php script would update the timestamp, and the current energy count? ??? Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-970898 Share on other sites More sharing options...
blueman378 Posted December 4, 2009 Share Posted December 4, 2009 your not actually updating the database based off the users timer, you are only making it "appear" so. you are infact only updating the database when the pages are requested again. the javascript is just to make it look nice. the idea i first said will handle everything to do with the database and updating the energy. Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-970916 Share on other sites More sharing options...
jeremyhowell Posted December 4, 2009 Author Share Posted December 4, 2009 Oh, I get it now... I gues it is not absolutely necessary to update the energy live... thanks you so much. Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-970976 Share on other sites More sharing options...
jeremyhowell Posted December 5, 2009 Author Share Posted December 5, 2009 I am having a problem with this code, it appears that it is setting the value of the curenergy field back to 1 every time it updates, and I have not the foggiest idea why. I really need some help here: $con = mysql_connect("localhost","root",""); mysql_select_db("users",$con); $result = mysql_query("SELECT * FROM users WHERE facebookid='100000372257424'"); $row = mysql_fetch_array($result); $lastupdate = $row['timereg']; // the time of the last update $maxenergy = $row['maxenergy']; $curenergy = $row['curenergy']; echo $curenergy."<br>"; $curtime = time(); $dif = $curtime-$lastupdate; echo time()."<br>"; while($dif>60) { $earned=$earned+1; $lastupdate=$lastupdate+60; $dif = $curtime-$lastupdate; mysql_query("UPDATE users SET timereg = '".$lastupdate."' WHERE facebookid='100000372257424'"); $currentenergy = $earned+curenergy; mysql_query("UPDATE users SET curenergy = '".$currentenergy."' WHERE facebookid='100000372257424'"); } $timeleft = round((($curtime-$lastupdate)/60),2); echo $timeleft; ?> ~ Jeremy Quote Link to comment https://forums.phpfreaks.com/topic/183676-please-help-with-a-phpjavascript-countdown-timer/#findComment-971686 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.