简体   繁体   中英

How to make a countdown/Timer for a game PHP/JS

I have created many text based webgames in my days, and I always used PHP, JS and MySQL.

One thing I never understood is how to create the "timer" between actions. Lets say it's a game where you rob banks. Once you click "Rob bank" a countdown starts, for lets say 2 minutes. And under those 2 minutes you can not Rob the bank again until those 2 minutes has passed. Is it possible to make this kind of countdown/timer system with php & javascript?

Can anyone point me in the right direction?

You have to save a timestamp somewhere on your server, and send it to your clients. Let's take the example with the bank:

I assume you have multiple banks, and stored them in a database, is that correct? If so, once a user starts robbing this bank, you set a startRobTimestamp to this bank. The users now can only rob this bank again, if the current timestamp is 2minutes bigger than the startRobTimestamp .

This is of course very basic, but the exact implementation depends very much on your data handling.

This might help: http://www.sitepoint.com/build-javascript-countdown-timer-no-dependencies/

You can then monitor the actual variables and keep robbing banks disabled till all are equal to 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM