Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
[Java Script] Countdown
Old 02-14-2007, 09:12 AM [Java Script] Countdown
Junior Talker

Posts: 2
Name: Michal Kalinowski
Trades: 0
Hi!
I want to make script, wich will subcract 1 from number (for ex. 145) every second and after 145 seconds will print "END OF TIME".

I found such script but it works only on one SPAN:

Code:
         <script language=javascript>
         function countdown(n)
         {
            if (n==0)
            {
               document.getElementById('cos').innerHTML="END OF TIME";
            }
            else
            {
               n--
               s=n%60
               m=Math.floor((n%3600)/60)
               g=Math.floor(n/3600)
               document.getElementById('cos').innerHTML=""+g+":"+((m<10)?"0"+m:m)+":"+((s<10)?"0"+s:s)+"";
               if(n>=0)setTimeout("countdown("+n+")",1e3);
            }
         }
         </script> 


<body onload="countdown(145)">TIME:<span id="cos"></span>
How I can change this code that it works with two or more SPANs?
I'm waiting for yours help, please.

P.S. Sorry for my english, but I'm foregein.
kalinowski5 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-14-2007, 06:30 PM Re: [Java Script] Countdown
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,515
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
how exactly do you want it to work with more span elements?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-14-2007, 06:45 PM Re: [Java Script] Countdown
Junior Talker

Posts: 2
Name: Michal Kalinowski
Trades: 0
I'm making game via Internet browser. I made php script wich return a few times (for example 75sec, 40sec and 123sec), so I need 3 span elements with diffrent times.
kalinowski5 is offline
Reply With Quote
View Public Profile
 
Old 02-15-2007, 04:07 PM Re: [Java Script] Countdown
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Quote:
Originally Posted by kalinowski5 View Post
I'm making game via Internet browser. I made php script wich return a few times (for example 75sec, 40sec and 123sec), so I need 3 span elements with diffrent times.
Since you are making a game, I'm going to suggest having a single timer element, as having lots of timeout/interval's running could get to be a pain to debug and could make some browsers really suffer.
I've attached a file with a demo (just change the extension from txt to html).
Note: for just simple count-downs the code is probably overkill

It has a single timer/interval object that starts/stops as listeners are added. The listeners are called at each interval 'tick'.
I created a SecondTimeListener object that will call a specified onChange function only when at least a second has passed.

Anyway, by using only one timer you can maintain performance of your javascript application while using it to handle animation of multiple sprites, timers and whatever other fun things you can come up with.

The window.onload stuff at the bottom kicks everything off. I've got the timer on a 50 millisecond interval (remember, the display only updates every second), you can change it if needed. See the interval property of the Timer object.
Attached Files
File Type: txt countdown.txt (3.5 KB, 2 views)
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 02-15-2007, 06:06 PM Re: [Java Script] Countdown
memberpro's Avatar
Super Talker

Posts: 143
Trades: 0
To use more than one span you must add a reference to another id in your javascript code.
Right now the program gets the value of the html of id=cos and then peforms the checks. What you will need to do is to is add aditional code to change the value of a span that you name a different id... for instance id=time1
Here would be the changes in the code to put two timers that are identical
Quote:
Originally Posted by kalinowski5 View Post
Code:
<script language=javascript>
function countdown(n)
{
if (n==0)
{
document.getElementById('cos').innerHTML="END OF TIME";
document.getElementById('time1').innerHTML="END OF TIME";
}
else
{
n--
s=n%60
m=Math.floor((n%3600)/60)
g=Math.floor(n/3600)
document.getElementById('cos').innerHTML=""+g+":"+((m<10)?"0"+m:m)+":"+((s<10)?"0"+s:s)+"";
document.getElementById('time1').innerHTML=""+g+":"+((m<10)?"0"+m:m)+":"+((s<10)?"0"+s:s)+"";
if(n>=0)setTimeout("countdown("+n+")",1e3);
}
}
</script> 
<body onload="countdown(145)">TIME:<span id="cos"></span>
<span id="time1"></span>
enjoy
__________________

Please login or register to view this content. Registration is FREE
- step-by-step learn how to design, create and install your own website in hours...not days.
Please login or register to view this content. Registration is FREE
was never so easy.

Last edited by memberpro; 02-15-2007 at 06:12 PM.. Reason: timed out browser again!!!
memberpro is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to [Java Script] Countdown
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.52425 seconds with 13 queries