PDA

View Full Version : Countdown Script Help



Scotty O
11-21-2005, 05:10 AM
How do you format leading zeros?

I would like to make it like:

"ddd:hh:mm:ss Left to the Scotty O Poker Challenge"

Thanks

Scotty O
ScottyOWeb.com (http://www.scottyoweb.com)

jscheuer1
11-21-2005, 06:58 AM
How do you want to format them? Generally, you query the length attribute of the number, if it is less than the number of digits you are looking for, you can add them but make sure to quote them or else they will be treated as numbers and zero + anything= the same number. I like to add them but, in a span preceding the number itself, that way I can make them invisible if I want, preventing the text from jumping around but not giving that 001 look that is not very real for humans.

To query the length of a number and pad it with leading zeros to a certain length:


while (numVar.length<3)
numVar='0'+numVar

This may turn your number into a string though so, if you need to use it later in math, you will have to change it back to a number using:


numVar=Math.abs(numVar)

or just save it as another variable and use that:


var holder=numVar
while (numVar.length<3)
numVar='0'+numVar
//code here to use numVar in a text display
numVar=holder

jscheuer1
11-21-2005, 07:57 AM
Here's a little demo that may help:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Number Padding</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">

function checkpads(num, len){
if (isNaN(num)){
alert ('Numbers Only Please')
return 0;
}
if (len==0){
alert ('Please Select a Padding Length')
return 0;
}
if (num.length>=len){
alert ('Number is already that Long or Longer')
return 0;
}
return 1;
}

function visiblepad(num, len){
if (!checkpads(num, len))
return '';
var padded=num
while (padded.length<len)
padded='0'+padded
return padded
}

function invisiblepad(num, len){
if (!checkpads(num, len))
return '';
var padded=''
while (padded.length+num.length<len)
padded+='0'
return '<span style="visibility:hidden;">'+padded+'</span>'+num
}

</script>
</head>
<body>
<form>
Number to be padded:
<input type="text" onchange="if (!isNaN(this.value)) document.getElementById('numarea').innerHTML=this.value;" onfocus="this.value=''" value="[Enter Number]">&nbsp;
Padding:
<select>
<option selected value="0">[Select Padding]
<option value="2">Pad to Two
<option value="3">Pad to Three
<option value="4">Pad to Four
</select>

<br>
<input type="button" value="Pad Number" onclick="document.getElementById('numarea').innerHTML=visiblepad(this.form[0].value, this.form[1].options[this.form[1].selectedIndex].value)">
<input type="button" value="Invisible Pad" onclick="document.getElementById('numarea').innerHTML=invisiblepad(this.form[0].value, this.form[1].options[this.form[1].selectedIndex].value)">
</form>
Landing Area:<br>
<div id="numarea"></div>
</body>
</html>