Javascript timer

Discussion in 'JavaScript' started by Sulan84, Sep 5, 2010.

  1. #1
    Hi. Here is code for a simple JavaScript Timer.
    My Problem is, that the soconds are junping back to "0" instead of "60,61,62...".

    How can modify this code, so that the seconds are not jumping back to "0" after "59".

    
    var zeitintervall	=	null;
    var timer		=	null;
    function Uhr()
    	{
    	var Jetzt 	=	new Date(); 
    	var myTime 	= 	new Date(Jetzt - StartTime);
    	var seconds	=	myTime.getSeconds();
    	if(seconds < 10){ seconds = "0"+seconds; }
    	var millis	=	myTime.getMilliseconds().toString();
    	document.getElementById('counter').innerHTML = seconds + ':' + millis.substr(0,2);
    	timer=setTimeout("Uhr()", 20);
    	zeitintervall = myTime;
    	}
    function StartStop(){
    		StartTime 	= 	new Date();
    		timer		=	setTimeout("Uhr()", 20);
    	}
    
    PHP:

     
    Sulan84, Sep 5, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Try not to use the Date( ) object, and just manually count
     
    Rainulf, Sep 5, 2010 IP
  3. Sulan84

    Sulan84 Member

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #3
    But i need the milliseconds...
     
    Sulan84, Sep 6, 2010 IP
  4. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Replace
    var seconds = myTime.getSeconds();

    With
    var seconds = myTime.getHours()*3600 + myTime.getMinutes()*60 + myTime.getSeconds();

    There may be a better way of doing it, I'm still learning this stuff :)
     
    Cash Nebula, Sep 13, 2010 IP