Lukela Posted January 12, 2008 Share Posted January 12, 2008 I want to create a script which will start a countdown, and when it hits 0:00:00 it will run a script. Anyone know how? Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 I want to create a script which will start a countdown, and when it hits 0:00:00 it will run a script. Anyone know how? Sounds like you're looking at Javascript. PHP is server-side only. Javascript is what you'll need, as its client-side. Quote Link to comment Share on other sites More sharing options...
Lukela Posted January 12, 2008 Author Share Posted January 12, 2008 Anyone know of any Javascript that can do this? I will implement them together. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 12, 2008 Share Posted January 12, 2008 Sounds like you're looking at Javascript. PHP is server-side only. Javascript is what you'll need, as its client-side. PHP can do this as well, with the sleep function: example from php.net: <?php // current time echo date('h:i:s') . "\n"; // sleep for 10 seconds sleep(10); // wake up ! echo date('h:i:s') . "\n"; ?> you could use this for pretty much anything: <?php echo "Bob: this is fun"; sleep(10); echo "Tom: yes it is"; ?> Regards ACE Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 Sounds like you're looking at Javascript. PHP is server-side only. Javascript is what you'll need, as its client-side. PHP can do this as well, with the sleep function: example from php.net: <?php // current time echo date('h:i:s') . "\n"; // sleep for 10 seconds sleep(10); // wake up ! echo date('h:i:s') . "\n"; ?> you could use this for pretty much anything: <?php echo "Bob: this is fun"; sleep(10); echo "Tom: yes it is"; ?> Regards ACE Try 'overwriting the same data' however, as a countdown is normally done. Edit: Re-read first post. Question: Did you want it to display a countdown or what-not? Quote Link to comment Share on other sites More sharing options...
Lukela Posted January 12, 2008 Author Share Posted January 12, 2008 The thing is that, what if they leave the page? Wont it restart if they go back? Plus what I am trying to do is to input the time into the database and make it so they can go browse the site and the timer goes down still. Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 The thing is that, what if they leave the page? Wont it restart if they go back? Plus what I am trying to do is to input the time into the database and make it so they can go browse the site and the timer goes down still. It might help if you explicitly tell us what you're trying to do. It can usually fill in a few blanks, and make things easier to solve. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 12, 2008 Share Posted January 12, 2008 true lol. If its a long term timer, such as days, weeks, or months. You could setup a cronjob to UPDATE a MySQL column on the current date and time and take that away from the starting time to get your count down timer dynamic. Regards ACE Quote Link to comment Share on other sites More sharing options...
Lukela Posted January 12, 2008 Author Share Posted January 12, 2008 I have this Browser-based game I am building with PHP and very basic Javascripts(cause I do not know). So when someone attacks, there is a time difference on how long it takes till there troops reach there target. That will be the timer, so when the timer hits 0 it will comense the attack script. Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 I have this Browser-based game I am building with PHP and very basic Javascripts(cause I do not know). So when someone attacks, there is a time difference on how long it takes till there troops reach there target. That will be the timer, so when the timer hits 0 it will comense the attack script. In that case, the PHP sleep function might be best. eg: <? echo "Walking to destination...<br />"; sleep(3); echo "Walking...<br />"; sleep(5); echo "Reached destination"; -- rest -- ?> Quote Link to comment Share on other sites More sharing options...
Lukela Posted January 12, 2008 Author Share Posted January 12, 2008 Is someone able to browse other pages while this is happening? Cause the attack can take hours, days, or minutes Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 12, 2008 Share Posted January 12, 2008 no, but I know what you mean now. I make Text Browser games myself. *looks at signature* I'm guessing you have a "attack" table in your database or something like that, which has "attacking user id", "defending user id" and the rest of it... what you need is another column in the attack table and name it something like "ETA"(estimated time of arrival) and.... I'm running low on ideas now... could do a cronjob to run every 5 mins or something to update the ETA column. but this will take away the accuracy of the ETA. Really depends on how long the estimated time of arrival, or attack is, like would you measure the time in hours, mins, secs or days?? EDIT: sorry just saw what you posted above... Is someone able to browse other pages while this is happening? Cause the attack can take hours, days, or minutes Regards ACE Quote Link to comment Share on other sites More sharing options...
Lukela Posted January 12, 2008 Author Share Posted January 12, 2008 Yeah If there was a way in PHP I think I woulda found out already I guess it is in JS my answer, it has to be very accurate. >.< -goes to google books to search for a Javascript for dummies book- Guess I got alot of learning to do! lolx Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted January 12, 2008 Share Posted January 12, 2008 lol, You can mix Javascript with PHP, use the javascript for the timer handling, and use PHP for the Database inserting... Regards ACE Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 no, but I know what you mean now. I make Text Browser games myself. *looks at signature* I'm guessing you have a "attack" table in your database or something like that, which has "attacking user id", "defending user id" and the rest of it... what you need is another column in the attack table and name it something like "ETA"(estimated time of arrival) and.... I'm running low on ideas now... could do a cronjob to run every 5 mins or something to update the ETA column. but this will take away the accuracy of the ETA. Really depends on how long the estimated time of arrival, or attack is, like would you measure the time in hours, mins, secs or days?? EDIT: sorry just saw what you posted above... Is someone able to browse other pages while this is happening? Cause the attack can take hours, days, or minutes Regards ACE Set a session variable for the expiration time, and one to say you're attacking. On page load of the attack script, check the session variable to see if its <= the current time AND that the one that says you're attacking is set. If both of those things are true, UNSET those variables and proceed with your attack script. Simple. As for displaying how long, basically: $time_to_go = $_SESSION['expire_time'] - time(); Quote Link to comment Share on other sites More sharing options...
Lukela Posted January 12, 2008 Author Share Posted January 12, 2008 How am I suppose to run the script when the timer reaches to 0? PHP cant do that, I also want people be able to browse the website. So the SESSION cant reload on its on, so thats why I need JS. Although I dont know any JS, -picks up book- anyone wanna save me the time? Im going to start learning JS though but I need the code asap no, but I know what you mean now. I make Text Browser games myself. *looks at signature* I'm guessing you have a "attack" table in your database or something like that, which has "attacking user id", "defending user id" and the rest of it... what you need is another column in the attack table and name it something like "ETA"(estimated time of arrival) and.... I'm running low on ideas now... could do a cronjob to run every 5 mins or something to update the ETA column. but this will take away the accuracy of the ETA. Really depends on how long the estimated time of arrival, or attack is, like would you measure the time in hours, mins, secs or days?? EDIT: sorry just saw what you posted above... Is someone able to browse other pages while this is happening? Cause the attack can take hours, days, or minutes Regards ACE Set a session variable for the expiration time, and one to say you're attacking. On page load of the attack script, check the session variable to see if its <= the current time AND that the one that says you're attacking is set. If both of those things are true, UNSET those variables and proceed with your attack script. Simple. As for displaying how long, basically: $time_to_go = $_SESSION['expire_time'] - time(); Quote Link to comment Share on other sites More sharing options...
twostars Posted January 12, 2008 Share Posted January 12, 2008 How am I suppose to run the script when the timer reaches to 0? PHP cant do that, I also want people be able to browse the website. So the SESSION cant reload on its on, so thats why I need JS. Although I dont know any JS, -picks up book- anyone wanna save me the time? Im going to start learning JS though but I need the code asap no, but I know what you mean now. I make Text Browser games myself. *looks at signature* I'm guessing you have a "attack" table in your database or something like that, which has "attacking user id", "defending user id" and the rest of it... what you need is another column in the attack table and name it something like "ETA"(estimated time of arrival) and.... I'm running low on ideas now... could do a cronjob to run every 5 mins or something to update the ETA column. but this will take away the accuracy of the ETA. Really depends on how long the estimated time of arrival, or attack is, like would you measure the time in hours, mins, secs or days?? EDIT: sorry just saw what you posted above... Is someone able to browse other pages while this is happening? Cause the attack can take hours, days, or minutes Regards ACE Set a session variable for the expiration time, and one to say you're attacking. On page load of the attack script, check the session variable to see if its <= the current time AND that the one that says you're attacking is set. If both of those things are true, UNSET those variables and proceed with your attack script. Simple. As for displaying how long, basically: $time_to_go = $_SESSION['expire_time'] - time(); Well if you have a header script that's included to every page (or executed before every page in any case), you can run the check then - if the page isn't the attack script, and the time is up -- header("Location: ./attack.php"); Quote Link to comment 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.