Page 1 of 1

Countdown timer

Posted: Tue Feb 17, 2009 11:54 am
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!

Re: Countdown timer

Posted: Tue Feb 17, 2009 12:15 pm
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>

Re: Countdown timer

Posted: Tue Feb 17, 2009 1:00 pm
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:

Re: Countdown timer

Posted: Tue Feb 17, 2009 1:28 pm
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.

Re: Countdown timer

Posted: Tue Feb 17, 2009 2:59 pm
by alpinec
Hi, which function to use so I can see the time of the server, not on the user's computer?

Re: Countdown timer

Posted: Tue Feb 17, 2009 3:38 pm
by John Cartwright

Code: Select all

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