Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Javascript Timer Help

  1. #1
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Timer Help

    Hello. I am a script Newbie. I want to place a timer on a page on my website. I already have a javascipt code that I want to use. But I'm afraid thats all I have at this point.

    The script is:
    Code:
    var time
    var output
    
    function Init(){
    	time = 1;
    	StartTimer()
    }
    
    function convertOutput(){
    	var hours = 0
    	var mins = 0
    	var secs = time
    	
    	while(secs >= 60){
    		secs -= 60;
    		mins += 1;
    	}
    	
    	if(secs < 10){
    		secs = "0" + secs;
    	}
    	
    	while(mins >= 60){
    		mins -= 60;
    		hours += 1;
    	}
    	
    	if(mins < 10){
    		mins = "0" + mins;
    	}
    	
    	output = hours + ":" + mins + ":" + secs;
    }
    
    function StartTimer(){
    	time += 1;
    	convertOutput();
    	document.getElementById("time").innerHTML = output;
    	self.setTimeout("StartTimer()", 1000);
    }
    I need to put this on a webpage on my website.

    Help will be greatly appreciated, and thanks in advance.
    Last edited by Twey; 01-25-2009 at 05:27 AM. Reason: Code formatting.

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

    Default

    What sort of a timer? We'll need a little more than this.
    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!

  3. #3
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Its an autostart timer that tells you how long you've been looking at a webpage (no buttons or user controls)

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

    Default

    Well, that's fairly easy:
    Code:
    <span id="timer">&nbsp;</span>
    
    <script type="text/javascript">
      (function(start) {
        function map(f, a) {
          for (var i = a.length - 1, r = []; i >= 0; --i)
            r[i] = f(a[i], i);
    
          return r;
        }
    
        function pad(s, w, c) {
          return (new Array(w + 1 - s.toString().length)).join(c || "0") + s;
        }
    
        function timerTick() {
          var now = new Date(new Date() - start);
          
          document.getElementById("timer").firstChild.nodeValue
            = map(function(n) { return pad(n, 2); },
                  [now.getUTCHours(),
                   now.getUTCMinutes(),
                   now.getUTCSeconds()]).join(":");
        }
    
        setInterval(timerTick, 1000);
      })(new Date());
    </script>
    Last edited by Twey; 02-02-2009 at 03:58 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!

  5. The Following User Says Thank You to Twey For This Useful Post:

    boogyman (02-03-2009)

  6. #5
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hmm, Do I just paste that into the page that I want the timer to be in? Because I just did and no timer.

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

    Default

    Should do, yes. If it didn't work, show the page so I can debug it.
    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!

  8. #7
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    the site is Forlol.com

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

    Default

    There is no <span id="timer"> on that page. You haven't placed the code properly.
    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!

  10. #9
    Join Date
    Jan 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    what code do I place and where do i place it

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

    Default

    Exactly the code I gave you, where you want the timer.
    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!

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
  •