/    Sign up×
Community /Pin to ProfileBookmark

Creating a countdown

i am completely new to JS… I’ve learnt the basics in about 15 minute
so i could create a countdown.

I guess i could finish it but the problem is i’ll have way too many IF statements
one in each other… i was hoping there is an easier way to do it…
here’s what i have done so far
(it probably looks silly but it’s the first thing i am creating with JS)
i’ll change the text boxes later into divs….

[CODE]

<input type=”text” id=”sec”>
<input type=”text” id=”min”>
<input type=”text” id=”hour”>
<input type=”text” id=”days”>
<script type=”text/javascript”>
countdown()
var rsec= <?php echo $time; ?>

var sec = rsec%60
var rmin = rsec/60
rmin = parseInt(rmin)

var min = rmin%60
var rhour = rmin/60
rhour = parseInt(rhour)

var hour = rhour%24
var rday = rhour/24
rday = parseInt(rday)

var day = rday
day = parseInt(day)

function countdown()
{
document.getElementById(‘min’).value=min
document.getElementById(‘sec’).value=sec
document.getElementById(‘hour’).value=hour
document.getElementById(‘days’).value=day
sec=sec-1

if (sec==-1)
{
sec = 59
min = min-1
if (min==-1)
{ // …… you can see how long it’s going to be if i continue
//this way, that’s why i am asking if there’s something simpler
// i could use.
}

}

t=setTimeout(“countdown()”,1000)
}

</script>

[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@slaughtersOct 15.2007 — Do it something like this (this is a snippet from a New Years Eve Countdown clock I did back in 1999) :

[CODE]function countdown() {

// Number of days in each month
var MonthLastDays = new Array();
MonthLastDays[1]=31;
MonthLastDays[2]=28;
MonthLastDays[3]=31;
MonthLastDays[4]=30;
MonthLastDays[5]=31;
MonthLastDays[6]=30;
MonthLastDays[7]=31;
MonthLastDays[8]=31;
MonthLastDays[9]=30;
MonthLastDays[10]=31;
MonthLastDays[11]=30;
MonthLastDays[12]=31;

// Parse out the different date components from todays date
var today = new Date();
var TodayYear = today.getYear();
var TodayMons = today.getMonth();
var TodayDays = today.getDate();
var TodayHour = today.getHours();
var TodayMins = today.getMinutes();
var TodaySecs = today.getSeconds();

TodayMons = TodayMons + 1 // For reasons unknown getMonth returns 0 thru 11 not 1 thu 12

// Subtract date components from last second before the end of the Year

YearLeft = 0;
MonsLeft = 12 - TodayMons;
DaysLeft= MonthLastDays[TodayMons] - TodayDays;
HourLeft = 23 - TodayHour;
MinsLeft = 59 - TodayMins;
SecsLeft = 59 - TodaySecs;

if (MonsLeft <= 0) {MonLeft = 0}
if (DaysLeft <= 0) {DaysLeft = 0}
if (HourLeft <= 0) {HourLeft = 0}
if (MinsLeft <= 0) {MinsLeft = 0}
if (SecsLeft <= 0) {SecsLeft = 0}

printcountdown();

// Update the clock again after 1 second

timerCD = setTimeout('countdown()',1000);

}[/CODE]



Create your own printcountdown() function to display the remaining months/days/hours/minutes/secnds the way you want to
Copy linkTweet thisAlerts:
@Declan1991Oct 15.2007 — Only one nested if is nothing really, when things get more complex you will have at least that. The only thing to watch out for is when to use switch and nested if elses.
Copy linkTweet thisAlerts:
@aleminioauthorOct 15.2007 — the only problem is, you're getting the time of the client computer.

i need the server time...

but i've got the idea and i'll modify it a little.

thank you.
Copy linkTweet thisAlerts:
@Declan1991Oct 15.2007 — Have you PHP support?
×

Success!

Help @aleminio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...