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 4 of 4

Thread: Timer

  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Timer

    My new website will be a "game" and therefore it will need a timer to run through months of the year.

    How can I run a timer to increment the date on an interval in javascript?

    Also how can I on a month say "January" call another script?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Ummm...if you are asking these questions, then I really think writing such a game is beyond you at this point in your learning curve.

    Code:
    var curmonth = 0;  // 0 is January in JavaScript
    
    function bumpMonth( )
    {
        ++curmonth;
        if ( curmonth == 12 )
        {
            curmonth = 0;
            doJanuary( );
        }
    }
    
    doJanuary();  // it's january at the start, so call this to begin
    setInterval( bumpMonth, 5000 ); // 5 seconds per month?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...if you are asking these questions, then I really think writing such a game is beyond you at this point in your learning curve.

    Code:
    var curmonth = 0;  // 0 is January in JavaScript
    
    function bumpMonth( )
    {
        ++curmonth;
        if ( curmonth == 12 )
        {
            curmonth = 0;
            doJanuary( );
        }
    }
    
    doJanuary();  // it's january at the start, so call this to begin
    setInterval( bumpMonth, 5000 ); // 5 seconds per month?
    What would you know. I that's a very gross assumption by you.

    You have no idea what my background skill and knowledge is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    But you asked two very basic questions. One which I'd expect anybody who intended to write a game would have long since found the answers to.

    For example, in any pseudo-real-time game, you surely would have encountered setInterval or setTimeout as almost the first step.

    But okay, I'll grant that you have experience in other aspects of writing games and apologize for my assumptions/doubts.

    Note that, despite my doubts, I did give you the skeleton code for what you wanted.

    You didn't say if you were going to roll over from year to year or just wanted a cycle of months. Adding in years would be trivial:
    Code:
    var curmonth = 0;  // 0 is January in JavaScript
    var curyear = 3032;
    
    function bumpMonth( )
    {
        ++curmonth;
        if ( curmonth == 12 )
        {
            ++curyear;
            curmonth = 0;
            doJanuary( );
        }
    }
    
    doJanuary();  // it's january at the start, so call this to begin
    setInterval( bumpMonth, 5000 ); // 5 seconds per month?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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