Results 1 to 3 of 3

Thread: Countdown Script Help

  1. #1
    Join Date
    Nov 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Countdown Script Help

    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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:

    Code:
    numVar=Math.abs(numVar)
    or just save it as another variable and use that:

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's a little demo that may help:

    Code:
    <!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>
    Last edited by jscheuer1; 11-21-2005 at 10:34 AM. Reason: add minor enhancement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •