Countdown timer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alpinec
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 11:40 am

Countdown timer

Post by alpinec »

Hello everyone. My name is Emil Nikolov(you can call me Emo). I`m from Bulgaria and this is my first post in this wonderful forum. I would to excuse me to my BAD english (i`m only 16 years old).

I`m developing some military web based game on PHP/MySQL and i need some countdown timer.
If you are play web based game you must know. There is a JavaScript timers in building menu. When you chose a building or research and click them, this timer start ticking and countdown. And when the countdown is finished the building has appear.


Timer should not be influenced by recharge or leaving the page or site. Might be saved in a database, no idea. Thank you in advance!
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Countdown timer

Post by papa »

Hi Emo and welcome to the forum!


I did a simple countdown timer a week ago:

Code: Select all

 
<script type="text/javascript">
//Timer Script
var s = 59;
var m = 0;
    
function timer() {
    var total_secs;
    //Add 0 before sek if below 10
    if(s<10) sek = '0' + s;
    else sek = s;
    //Display Timer
    document.getElementById('timer').innerHTML= 'Time: ' + m + ':' + sek;
    //Increment Second
    s--;
    total_secs--;
    //Second to Minute
    if(s==60) {
        //Reset second
        s=0;
        //Add minute
        m++;
    }
 
    if(s==0) { 
        alert('Time\'s Up!');
    }
}
//Initiate timer
setInterval("timer();", 1000); 
</script>
 
You can change these:
//Increment Second
s--;
total_secs--;

to
s++
total_secs++;

If you want it to increment (which is the original).

Also add a div element with id 'timer'
<div id="timer"></div>
alpinec
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 11:40 am

Re: Countdown timer

Post by alpinec »

Thanks man, that is what i`m looking for. There was 1 mistake. I lil continue to work on this code and if a have some issues i lil write here.... and soon my first game will be done! Have a nice day man, and thank you again! :) :drunk:
alpinec
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 11:40 am

Re: Countdown timer

Post by alpinec »

Hi, I met a problem already. When you refresh the page count starts again as I entered in the script. I can easily do it to remember where it is reached before a reload or refresh the page. But then the problem will be that while the page count is not counting closed time!

I forgot to say that almost nothing know JavaScript. Currently reading a book on AJAX.
alpinec
Forum Newbie
Posts: 5
Joined: Tue Feb 17, 2009 11:40 am

Re: Countdown timer

Post by alpinec »

Hi, which function to use so I can see the time of the server, not on the user's computer?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Countdown timer

Post by John Cartwright »

Code: Select all

echo date('m-d-y');
Post Reply