Results 1 to 6 of 6

Thread: refresh time js

  1. #1
    Join Date
    Oct 2007
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default refresh time js

    I am aiming at having a time (in a function) that refreshes itself every 10 seconds using JS but am having trouble. The time is shown but doesn't update. The page seems to be refreshing though.

    Here is my code
    Code:
    <script type="text/javascript">
    function theTime() {
    	var date = new Date;
    	var hours = date.getHours();
    	var minutes = date.getMinutes();
    	if(hours > 11){
    		var ext = "PM";
    	} else {
    		var ext = "AM";
    	}
    	if (hours > 12) {
    		hours = hours - 12;
    	}
    	if (minutes < 10){
    		minutes = "0" + minutes;
    	}
    document.write(hours + ":" + minutes + ext)
    }
    function refreshPage(refresh)
    {
    	setTimeout('theTime();',refresh)
    }
    refreshPage(10000);
    </script>
    Help much appreciated.
    magik

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This should do it:
    Code:
    <script type="text/javascript">
    var timein = function() {
    	var date = new Date()
    	var month = date.getMonth()+1;
    	var hours = date.getHours();
    	var ext;
    	if(hours > 12){
    		hours-=12;
    	}
    	if(hours == 0) hours = 12;
    	if(hours > 11){
    		ext = "PM";
    	} else {
    		ext = "AM";
    	}
    	var dateout = hours+":"+date.getSeconds()+ext;
    	document.getElementById('dt').innerHTML=dateout;
    }
    setInterval("timein()",10000);
    window.onload = function () { timein(); };
    </script>
    <div id="dt"></div>
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript">
        Number.prototype.pad = function(n) {
            return (new Array(1 + n - this.toString().length)).join("0") + this;
        };
    
        Date.prototype.getTwelveHourStamp = function() {
            var hours = this.getHours(),
                mins  = this.getMinutes().pad(2),
                ext   = hours >= 12 ? "PM" : "AM";
    
            return (hours % 12) + ":" + mins + ext;
        };
    
        setInterval((function(f) { f(); return f; })
            (function() { document.getElementById("clock").firstChild.nodeValue = (new Date()).getTwelveHourStamp(); }), 10000);
    </script>
    Have an element somewhere to catch it:
    Code:
    <span id="clock">&nbsp;</span>
    Place the Javascript code after the element.

    Edit: Nile: try to avoid innerHTML, it's non-standard and leads to horrible code if used in large quantities. Additionally, eval() (which is what happens behind the scenes just about wherever you see a string of Javascript code) is very slow and leads to most of the same problems. Please read my list of common coding mistakes and the links therein.
    setInterval("timein()",10000);
    window.onload = function () { timein(); };
    The former would be better written as setInterval(timein, 10000), the latter as onload = timein.
    Last edited by Twey; 12-22-2008 at 06:39 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ok, thanks! I'll read your mistakes list and keep your tips in mind, also here:
    Code:
        Date.prototype.getTwelveHourStamp = function() {
            var hours = this.getHours(),
                mins  = this.getMinutes().pad(2);
                ext   = hours >= 12 ? "PM" : "AM";
    
            return (hours % 12) + ":" + mins + ext;
        };
    Shouldn't the highlighted be a comma (',')?
    Jeremy | jfein.net

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Should indeed. Thanks for that! *edits*
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    [offsubject]Your welcome, and thanks for the tips. I just read your "common coding mistakes."[/offsubject]
    Jeremy | jfein.net

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
  •