PDA

View Full Version : [DHTML] Unlimited Timer



mburt
04-06-2007, 09:29 PM
1) CODE TITLE: Unlimited Timer

2) AUTHOR NAME/NOTES: Michael Burt (http://mburt.funpic.org)

3) DESCRIPTION: This timer script allows you to have as many terms as you want, ex: 5:40:23:14. Or even 0:14, which would be fourteen seconds, it will count down to 0:00.

4) URL TO CODE/DEMO: http://mburt.funpic.org/testing/countdown.htm

Twey
04-06-2007, 09:32 PM
What would 15:14:13:12:11:10:9:8:7:6:5:4:3:2:1 be? One second, two minutes, three hours, four days, five weeks, six months, seven years, eight decades, nine centuries, ten millennia? Then what?

mburt
04-06-2007, 09:43 PM
Hmm... It's possibilities are endless, "15:14:13:12:11:10:9:8:7:6:5:4:3:2:1" would still be valid :p. I'm not sure what's after ten millenia though.

Twey
04-06-2007, 10:29 PM
You've mutilated my Number.prototype.pad() function :p It can be written much more neatly as:
Number.prototype.pad = function(n) {
for(var r = this.toString(); r.length < n; r = 0 + r);
return r;
};Also, you should avoid using innerHTML, and you should object-orient the script in case someone wants more than one on the page.

Other than that, however, very good -- I understand how it works now (I'd only glanced at it before) :) It's a variation on the base-26 problem I saw here recently.

mburt
04-06-2007, 10:41 PM
I tried using that function, but it didn't work... At all. I think It was trying to read it as a string or something.

After spending two hours coding it, I was way too lazy to replace innerHTML with:

while (el.firstChild) el.removeChild(el.firstChild);
el.appendChild(document.createTextNode(x.substring(0,x.length-1)+fext;))

Whoa... I just did. :p

mburt
04-06-2007, 10:44 PM
Oops. There's a syntax error... Stupid semi-colens.

Twey
04-06-2007, 10:46 PM
Hmn, works here. I was doing r = r + 0 instead of r = 0 + r in the one above (edited now) but I'm fairly sure I don't usually make that mistake.

mburt
04-06-2007, 10:52 PM
Actually, before you had r = "0" + r as a string.

Twey
04-06-2007, 10:58 PM
No matter, it's type-coerced into a string anyway.

mburt
04-06-2007, 11:01 PM
Right... I forgot the whole .toString() bit.

boogyman
04-09-2007, 04:18 AM
couldn't you do

r += "0"

?

Twey
04-09-2007, 12:40 PM
No, it needs to go on the front of the string. += appends.

mburt
04-09-2007, 01:18 PM
Exactly.

Output for pissa's example, say if the number is 5:

50
r = "0"+r;

05

boogyman
04-09-2007, 01:43 PM
oh sorry, I didnt' think you were talking about a zero fill, sorry

mburt
04-09-2007, 01:45 PM
No big deal, just a matter of which operator and sequence of variables/strings to use.