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: date loop

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

    date loop

    i'm trying to get my date in increase my 1 month on a set interval but it does not work, what can I do here?

    Code:
    <script type="text/javascript">
    
    var myDate = new Date(1960,1,1);
    var myVar=setInterval(function(){myTimer()},1000);
    
    function myTimer(){
    	var nDate = new Date(myDate.getMonth()+1);
    	alert(nDate);
    
    }
    
    </script>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Code:
    function myTimer(){
    	myDate.setMonth(myDate.getMonth()+1);
    	alert(myDate);
    }

  • #3
    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
    Better write the output to a <span> rather than use an alert, as otherwise with one second interval you will not be able to stop it without restarting the browser.

    Code:
    <span id = "theDate"></span>
    
    <script type="text/javascript"> 
    var myDate = new Date(1960,1,1); 
    document.getElementById("theDate").innerHTML = myDate;
    setInterval(myTimer,1000) 
    
    function myTimer(){
    myDate.setMonth(myDate.getMonth()+1);
    document.getElementById("theDate").innerHTML = myDate;
    	
    }
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    Better write the output to a <span> rather than use an alert, as otherwise with one second interval you will not be able to stop it without restarting the browser.
    That's why there's an extra checkbox in the bottom of the alert in some browsers - so you can stop the constant debugging alerts from popping up without having to restart the browser.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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