Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Counter script not working

    Hi, I am trying to make this script work as a population counter but I don't know how to use Java. Can you please help me with any suggestions on how to make it work? I have already been trying for three days..

    Thank you,

    Christian

    Code:
    <div id = "AustPop"></div>
    
    <script type = "text/javascript">
    
    var basisDate = new Date(2009,4,24,05,46,36).getTime();   // 24 May 2009 at 05:46:36 (Canberra Time)
    var basisPop = 21769052;  // population as at that time
    
    function ozPop() {
    var now = new Date();
    var now = new Date().getTime();
    var diff = (now - basisDate)/60000;  // minutes
    var newPop = parseInt((diff/1.5) + basisPop);  // increase by 1 every 1.5 minutes
    newPop = newPop.toString();
    newPop = addCommas(newPop);  // format with commas
    msg = "The population of Australia as of this moment is estimated to be:-  ";
    document.getElementById("AustPop").innerHTML = msg + newPop;
    
    window.setTimeout("ozPop()",30000);  // update each half minute
    }
    
    function addCommas(nStr) {
    nStr = nStr.replace(/[^0-9\.]/g,"");
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1,$2');
    }
    return nStr;
    }
    
    </script>
    Last edited by christiang; 05-24-2009 at 12:32 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    It works fine for me. What is the problem?

    Have you called it with
    <body onload = "ozPop()">

    Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

    And why have you started a new thread?
    Last edited by Philip M; 05-24-2009 at 09:04 AM.

  • Users who have thanked Philip M for this post:

    christiang (05-24-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Thanks

    Hi Philip,

    The script you wrote works perfectly thank you very much for you help. Regardless of whether or not it worked, I absolutely appreciate and understand that you made a specific effort to help me.
    I hadn't put <body onload = "ozPop()"> at the beginning, I had tried putting <body>, but like I said, I've never used JavaScript before so what may be obvious to a professional like yourself is difficult for me. Also, the reason I made a new thread was because the original thread didn't have a post icon, and the title wasn't bold anymore, it seemed as though it had been marked as resolved..(I am also new to this forum and have since read the 'posting guidelines.')

    Again, thank you very much for taking the time to help.

    Christian


  •  

    Posting Permissions

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