Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   IT / Communications (http://www.chiefdelphi.com/forums/forumdisplay.php?f=85)
-   -   javascript ate my cereal! (http://www.chiefdelphi.com/forums/showthread.php?t=27274)

steveg 29-03-2004 00:15

javascript ate my cereal!
 
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?

Code:

<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','Aug','Sep','Oct','Nov','Dec')

function countdown(yr,mth,day,hour,min,sec)
{
        theyear=yr;themonth=mth;theday=day;thehr=hour;themin=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,themin,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

Re: javascript ate my cereal!
 
Quote:

Originally Posted by steveg
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):

Code:

<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))%(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()', 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

Re: javascript ate my cereal!
 
Awsome, thank you!

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


All times are GMT -5. The time now is 10:44.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi