/    Sign up×
Community /Pin to ProfileBookmark

What’s wrong with this code?

hi,

I’m trying to get this script to work for two instances as shown:

[url]http://releasemelissa.com/js/timeservedtest.html[/url]

As can be seen, it works fine with the first instance but not the second.

The JS file is here:

[url]http://releasemelissa.com/js/timeserved_dev.js[/url]

Thanks =)

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJul 09.2009 — [CODE]<html>
<head>
<title>timeserved test</title>
<link rel="stylesheet" href="/css/main.css" type="text/css" />
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/

var imgPath = 'http://releasemelissa.com/images/';
var imgPrefix = 'tally';
var imgExt = '.gif';
var imgH = 20;
var imgW = 25;

function Tally(id,since){
var obj=document.getElementById(id);
this.ary=obj.getElementsByTagName('dd');
this.since=since;
this.id=id
this.update();
}

Tally.prototype.update=function(){
var now = new Date();
var diffMilliseconds = now.getTime() - this.since.getTime();

var seconds = 0;
var minutes = 0;
var hours = 0;
var days = 0;
var months = 0;
var years = 0;

seconds += now.getSeconds() - this.since.getSeconds();

if (seconds < 0)
{
seconds += 60;
minutes--;
}
minutes += now.getMinutes() - this.since.getMinutes();
if (minutes < 0)
{
minutes += 60;
hours--;
}
hours += now.getHours() - this.since.getHours();
if (hours < 0)
{
hours += 24;
days--;
}
days += now.getDate() - this.since.getDate();
if (days < 0)
{
days += daysInMonth(this.since.getFullYear(), this.since.getMonth());
months--;
}
months += now.getMonth() - this.since.getMonth();
if (months < 0)
{
months += 12;
years--;
}
years += now.getFullYear() - this.since.getFullYear();

this.ary[0].innerHTML = this.ToTally(years);
this.ary[1].innerHTML = this.ToTally(months);
this.ary[2].innerHTML = this.ToTally(days);
this.ary[3].innerHTML = this.ToTally(hours);
this.ary[4].innerHTML = this.ToTally(minutes);
this.ary[5].innerHTML = this.ToTally(seconds);
var oop=this;
this.to=setTimeout(function(){ oop.update(); },1000);
}

function ToTallyDigit(num){
if (!num) return "";
return '<img src="' + imgPath + imgPrefix + num + imgExt + '" width="' + imgW + '" height="' + imgH + '" alt=""/>';
}

Tally.prototype.ToTally=function(num){
var html = "";
for (i = 1; i < (num + 1) / 5; i++){
html += ToTallyDigit(5);
}
html += ToTallyDigit(num % 5);
return html;
}

//http://snippets.dzone.com/posts/show/2099
function daysInMonth(iYear, iMonth)
{
return 32 - new Date(iYear, iMonth, 32).getDate();
}


/*]]>*/
</script>
</head>
<body>

<div class="timeServed" id="timeServed0">
<dl>
<dt>Years</dt>
<dd id="timeServed0Years"></dd>
<dt>Months</dt>

<dd id="timeServed0Months"></dd>
<dt>Days</dt>
<dd id="timeServed0Days"></dd>
<dt>Hours</dt>
<dd id="timeServed0Hours"></dd>
<dt>Minutes</dt>
<dd id="timeServed0Minutes"></dd>

<dt>Seconds</dt>
<dd id="timeServed0Seconds"></dd>
</dl>
</div>
<script>
var tsId="timeServed0";
</script>
<div class="timeServed" id="timeServed1">
<dl>
<dt>Years</dt>

<dd id="timeServed1Years"></dd>
<dt>Months</dt>
<dd id="timeServed1Months"></dd>
<dt>Days</dt>
<dd id="timeServed1Days"></dd>
<dt>Hours</dt>
<dd id="timeServed1Hours"></dd>

<dt>Minutes</dt>
<dd id="timeServed1Minutes"></dd>
<dt>Seconds</dt>
<dd id="timeServed1Seconds"></dd>
</dl>
</div>

<script type="text/javascript">
/*<![CDATA[*/

var T1=new Tally('timeServed0',new Date(2003,3,5,8,45));
var T2=new Tally('timeServed1',new Date(2003,3,5,8,45));

/*]]>*/

</script>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@jofloauthorJul 09.2009 — Thanks!
×

Success!

Help @joflo 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 5.17,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...