trouble with countdown javascript

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
percking
Posts: 2
Joined: Fri Jun 03, 2005 9:14 pm

trouble with countdown javascript

Post by percking »

I've got some javascript code that keeps a countdown till a certain date running on a webpage. It has worked for me in several other places but I can't make it work with serendipity. I've inserted into an HTML Nugget on my sidebar and it does nothing.

I've tracked it to what I think is issues with my javascript and the DOCTYPE statement (XHTML). In my reading I've found no good reason why it shouldn't work but I thought i'd ask on here to see if anybody knows what exactly is wrong with my javascript thats making it "uncompliant".

Here is the code:

Code: Select all

<script language="Javascript" type="text/javascript">
        <!--
        var now = new Date();
        var event = new Date("June 5 12:00:0");
        var seconds = (event - now) / 1000;
        var minutes = seconds / 60;
        var hours = minutes / 60;
        var days = hours / 24;
        ID=window.setTimeout("update();", 1000);
        function update()
        {
                now     = new Date();
                seconds = (event - now) / 1000;
                seconds = Math.floor(seconds);
                minutes = seconds / 60;
                minutes = Math.floor(minutes);
                hours   = minutes / 60;
                hours   = Math.floor(hours);
                days    = hours / 24;
                days    = Math.floor(days);
                document.getElementById(\'days\').innerHTML    = days;
                document.getElementById(\'hours\').innerHTML   = hours;
                document.getElementById(\'minutes\').innerHTML = minutes;
                document.getElementById(\'seconds\').innerHTML = seconds;
                document.getElementById(\'full\').innerHTML    = days + (days==1?" day ":" days ") + (hours-24*day\
s) + (\
hours-24*days==1?" hour ":" hours ") + (minutes - 60*hours) + (minutes-60*hours==1?" minute and ":" minutes and ")\
 + (\
seconds - minutes*60) + (seconds-minutes*60==1?" second":" seconds");
                ID=window.setTimeout("update();",1000);
        }
// -->
</script>
  <div id=full></div>
                <table>
                <tr>
                        <td align=right>Days:</td>
                        <td id=days align=right></td>
                </tr>
                <tr>
                        <td align=right>Hours:</td>
                        <td id=hours align=right></td>
                </tr>
                <tr>
                        <td align=right>Minutes:</td>
                        <td id=minutes align=right></td>
                </tr>
                <tr>
                        <td align=right>Seconds:</td>
                        <td id=seconds align=right></td>
                </tr>
                </table>
Any Help would be appreciated!!

M.G.

EDIT: Forget the slashes before the apostrophes and on line breaks, i copied it out of a putty/emacs session from the original place in some PHP code of mine.
percking
Posts: 2
Joined: Fri Jun 03, 2005 9:14 pm

Post by percking »

I found that, using Firefox's JavaScript Console, the line where I set the div named 'full' to the current time using lots of if elses was somehow broken by the DOCTYPE. Taking that line out for the moment left me with a script that half worked. It now prints NaN for each number (Not a Number). I think now it's something about dates that is messing it up.

Is there a good tutorial or something somewhere that can tell me what works with what types?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

You should try to put quotes around the "id" tags...apart from that I sadly don't know if/which things get triggered in the Non-Standard Compliant Browser modes...

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply