[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javscript stopwatch slow!!!

Posted on 2011-05-12
8
Medium Priority
?
300 Views
Last Modified: 2012-06-27
Hey guys,

Ok i have my stopwatch working ish....

I say ish because its slow! any idea why or how to pik up the speed to realtime?

var t;
var h = '00';
var m = '00';
var s = '00';
var hs = 0;
var dr;
var drcount = 1;

function timedCount() {

    document.getElementById('txt').value = h + ":" + m + ":" + s + ":" + hs;
    hs++;
    if (hs == 10) {
        hs = 00;
        s++;
        s = checkTime(s);
    }
    if (s == 60) {
        s = 00;
        m++;
        m = checkTime(m);
    }
    if (m == 60) {
        m = 00;
        h++;
        h = checkTime(h);
    }
    t = setTimeout("timedCount()", 100);




}

function checkTime(check) {

    if (check < 10) {
        check = '0' + check;
    }
    if (check == 0) {
        check = '00';
    }
    return check;

}





function resetTime() {

    clearTimeout(t);
    h = '00';
    m = '00';
    s = '00';
    hs = 0;
    drcount = 1;
    document.getElementById('txt').value = h + ":" + m + ":" + s + ":" + hs;

}

Open in new window

0
Comment
Question by:runnerjp2005
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:dmitry_n
ID: 35747828
Why did you count? Read timestamps and display a difference.
0
 

Author Comment

by:runnerjp2005
ID: 35747968
i do do that but the stopwatch is for show.. and it ticks through rlly slow
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35748405
Check the following to see if it help :
<html><head><script>
var c = 21500; // set it to zero

function timedCount() {
	t = setInterval(function() {
		c++;
		var h = parseInt(c/21600);
		var m = parseInt((c - (h * 21600))/360);
		var s = parseInt((c - (h * 21600) - (m * 360))/6);
		var k = (c - (h * 21600) - (m * 360) - (s * 6));
		document.getElementById('txt').innerHTML = h + ":" + d2(m) + ":" + d2(s) + ":" + k;
	}, 100); // 100ms
}

function d2(n) { return (n<10)?"0"+n:n; } 

function resetTime() {
	clearTimeout(t);
}

</script></head><body onLoad="timedCount()">
<span id="txt"></span>
</body></html>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:leakim971
ID: 35748616
Correction (line 9 and 10) :
<html><head><script>
var c = 21500; // set it to zero

function timedCount() {
	t = setInterval(function() {
		c++;
		var h = parseInt(c/21600);
		var m = parseInt((c - (h * 21600))/360);
		var s = parseInt((c - (h * 21600) - (m * 360))/10);
		var k = (c - (h * 21600) - (m * 360) - (s * 10));
		document.getElementById('txt').innerHTML = h + ":" + d2(m) + ":" + d2(s) + ":" + k;
	}, 100); // 100ms
}

function d2(n) { return (n<10)?"0"+n:n; } 

function resetTime() {
	clearTimeout(t);
}
</script></head><body onLoad="timedCount()">
<span id="txt"></span>
</body></html>

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 35748652
A new one :
<html><head><script>
var c = 35900; // set it to zero

function timedCount() {
	t = setInterval(function() {
		c++;
		var h = parseInt(c/36000);
		var m = parseInt((c - (h * 36000))/600);
		var s = parseInt((c - (h * 36000) - (m * 600))/10);
		var k = (c - (h * 36000) - (m * 600) - (s * 10));
		document.getElementById('txt').innerHTML = h + ":" + d2(m) + ":" + d2(s) + ":" + k;
	}, 100); // 100ms
}

function d2(n) { return (n<10)?"0"+n:n; } 

function resetTime() {
	clearTimeout(t);
}
</script></head><body onLoad="timedCount()">
<span id="txt"></span>
</body></html>

Open in new window

0
 
LVL 23

Expert Comment

by:cmalakar
ID: 35748740
Optimized version of your code.

Note that the method callOnLoad() should be called as window onload event.
function callOnLoad() {
       x = document.getElementById('check');
       timedCount();
}


var t;
var h = 0;
var m = 0;
var s = 0;
var hs = 0;
var dr;
var drcount = 1;
var x ;

function timedCount() {

    x.value = display(h) + ":" + display(m) + ":" + display(s) + ":" + display(hs);
    hs++;
    if (hs == 10) {
        hs = 00;
        s++;
        if(s == 60){
          s = 00;
          m++;
          if (m == 60) {
             m = 0;
             h++;
           }
        }
    }
    
    t = setTimeout("timedCount()", 100);
}


function display(val){
return val < 10 ? (val == 0 ? '00' : '0' + val) : val;
}



function resetTime() {

    clearTimeout(t);
    h = 0;
    m = 0;
    s = 0;
    hs = 0;
    drcount = 1;
    x.value = display(h) + ":" + display(m) + ":" + display(s) + ":" + display(hs);

}

Open in new window

0
 
LVL 23

Expert Comment

by:cmalakar
ID: 35748756
if you invoke document.getElementById('txt') for every turn, it going to take some time for execution.

You can replace that with only one time invocation as shown.
0
 
LVL 3

Accepted Solution

by:
dmitry_n earned 2000 total points
ID: 35753144
var t0 = new Date();

function timedCount() {

    var t1 = new Date() - t0;

    hs = parseInt(t1 % 1000 / 10)
    s = parseInt(t1 / 1000) % 60
    m = parseInt(t1 / (1000*60)) % 60
    h = parseInt(t1 / (1000*60*60))

    document.getElementById('txt').value = checkTime(h) + ":" + checkTime(m) + ":" + checkTime(s) + ":" + checkTime(hs);

    t = setTimeout("timedCount()", 100);
}
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question