Log in

View Full Version : javascript ate my cereal!


steveg
29-03-2004, 00:15
I'm trying to code a simple countdown timer for a friend, but the code as it is now will run once, then return NaNs for the numbers. Eventually it will start working under safari, but not under IE or Mozilla. Any suggestions?


<FORM name=count>
<INPUT size=73 name=count2>
</FORM>

<SCRIPT>

//Count down until any date and time


var until='Party'
var current='A Title of some sort to display when time is up'
var montharray=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','A ug','Sep','Oct','Nov','Dec')

function countdown(yr,mth,day,hour,min,sec)
{
theyear=yr;themonth=mth;theday=day;thehr=hour;them in=min;thesec=sec
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()

var todaystring=montharray[todaym]+' '+todayd+', '+todayy+' '+todayh+':'+todaymin+':'+todaysec

futurestring=montharray[mth-1]+' '+theday+', '+theyear+' '+thehr+':'+themin+':'+thesec


dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000) )/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000 ))%(60*1000))/1000*1)

if(dday==0&&dhour==0&&dmin==0&&dsec==0)
{
document.forms.count.count2.value=current
return
}
else
document.forms.count.count2.value='Only '+dday+ ' days, '+dhour+' hours, '+dmin+' minutes, and '+dsec+' seconds left until '+until

setTimeout('countdown(theyear,themonth,theday,them in,thesec)',1000)
}
//enter the count down date using the format year/month/day/hour/minute/seconds

countdown(2004,4,15,0,0,0)

</SCRIPT>

piotrm
29-03-2004, 00:43
I'm trying to code a simple countdown timer for a friend, but the code as it is now will run once, then return NaNs for the numbers. Eventually it will start working under safari, but not under IE or Mozilla. Any suggestions?


try this (sorry I had to add spaces to this so I could actually read it):


<html>
<body>
<FORM name=count>
<INPUT size=73 name=count2>
</FORM>
</body>
</html>
<SCRIPT language="javascript">
//Count down until any date and time
var until = 'Party';
var current = 'A Title of some sort to display when time is up';
var montharray =
new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var theyear;
var themonth;
var theday;
var thehr;
var themin;
var thesec;
function countstart(yr, mth, day, hour, min, sec) {
theyear = yr;
themonth = mth;
theday = day;
thehr = hour;
themin = min;
thesec = sec;
setTimeout('countdown()', 1000);
}
function countdown() {
var today = new Date();
var todayy = today.getYear();
if (todayy < 1000) {
todayy += 1900;
}
var todaym = today.getMonth();
var todayd = today.getDate();
var todayh = today.getHours();
var todaymin = today.getMinutes();
var todaysec = today.getSeconds();
var todaystring =
montharray[todaym]
+ ' ' + todayd
+ ', ' + todayy
+ ' ' + todayh
+ ':' + todaymin
+ ':' + todaysec;
futurestring =
montharray[themonth-1]
+ ' ' + theday
+ ', ' + theyear
+ ' ' + thehr
+ ':' + themin
+ ':' + thesec;
dd = Date.parse(futurestring) - Date.parse(todaystring);
dday = Math.floor(dd/(60*60*1000*24)*1);
dhour = Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1);
dmin = Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
dsec= Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(6 0*1000))/1000*1);
if (dday == 0 &&
dhour == 0 &&
dmin == 0 &&
dsec == 0) {
document.forms.count.count2.value = current;
return;
} else {
document.forms.count.count2.value=
'Only '
+ dday + ' days, '
+ dhour + ' hours, '
+ dmin + ' minutes, and '
+ dsec + ' seconds left until '
+ until;
}
setTimeout('countdown()', 1000);
}
//enter the count down date using the format year/month/day/hour/minute/seconds
countstart(2004,3,29,1,0,0);
</SCRIPT>

steveg
29-03-2004, 01:10
Awsome, thank you!

Though I don't know why it didn't work the way it was before.