treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Increment counter formatting

  • Basically the counter I have created for a website seems to be working as I want it to (I can edit it to do what I want) - but for some reason when I increment it by decimals i.e. 0.1, 0.2, 0.3 etc the formatting glitches?

    You can view the counter live at http://lowco2.eu

    Here is the code behind the counter:

    Script:

      > <script type="text/javascript">
    
          var number = <?=(int)((time()-1262325600)/150);?>; // put your initial value here
    
        function incrementNumber () {
            number += 0.1; // you can increment by anything you like here
          document.getElementById('total').innerHTML = number;
        }
    
        // this will run incrementNumber() every second (interval is in ms)
        setInterval(incrementNumber, 1000);
    
          function formatNumber(num) {
           num = String(num);
    
           if (num.length <= 3) {
              return num;
           } else {
              var last3nums = num.substring(num.length - 3, num.length);
              var remindingPart = num.substring(0, num.length - 3);
              return formatNumber(remindingPart) + ',' + last3nums;
           }
        }
    
        </script>
    

    Any help is much appreciated!

  • Can anyone help with this at all?

    Cheers

  • I'm not seeing a counter on the link you provided.

    EDIT: Wow that's hard to see.

  • I don't see the point behind having it on your website anyway as it's not really a true reflection on the amount you're exactly saving to be honest...

    But i just ran your code through JSLint on the JSFiddle website and it threw back a few errors.

    Try changing this;

    var number = <?=(int)((time()-1262325600)/150);?>;
    

    To this;

    var number = (int)((time()-1262325600)/150);
    

    This is what I got - Here

  • I'm not seeing a counter on the link you provided.

    EDIT: Wow that's hard to see.

    It isn't live hence it being hard to see, but thanks for your lack of help!

    Thanks Watson... I will give it a try and see if it begins to sort it out.

    It is possible to work out an estimate by taking the average tonnes of co2 they save over the last 3 years. I only design what they want though, so I won't defend the need of it!

    Cheers