Javascript chronometer/stopwatch
I needed a javascript chronometer/stopwatch to be inserted in a project, and I didn't find anything on the web that fit my idea, so I created one almost from scratch, with all functions a chronometer should have, such as milliseconds measurement, start, stop, lap time and so on (well... that's all).
Let's see the javascript
JAVASCRIPT:
-
<script language="JavaScript">
-
-
<!--
-
-
var timercount = 0;
-
var timestart = null;
-
-
function showtimer() {
-
if(timercount) {
-
clearTimeout(timercount);
-
clockID = 0;
-
}
-
if(!timestart){
-
timestart = new Date();
-
}
-
var timeend = new Date();
-
var timedifference = timeend.getTime() - timestart.getTime();
-
timeend.setTime(timedifference);
-
var minutes_passed = timeend.getMinutes();
-
if(minutes_passed <10){
-
minutes_passed = "0" + minutes_passed;
-
}
-
var seconds_passed = timeend.getSeconds();
-
if(seconds_passed <10){
-
seconds_passed = "0" + seconds_passed;
-
}
-
document.timeform.timetextarea.value = minutes_passed + ":" + seconds_passed;
-
timercount = setTimeout("showtimer()", 1000);
-
}
-
-
function sw_start(){
-
if(!timercount){
-
timestart = new Date();
-
document.timeform.timetextarea.value = "00:00";
-
document.timeform.laptime.value = "";
-
timercount = setTimeout("showtimer()", 1000);
-
}
-
else{
-
var timeend = new Date();
-
var timedifference = timeend.getTime() - timestart.getTime();
-
timeend.setTime(timedifference);
-
var minutes_passed = timeend.getMinutes();
-
if(minutes_passed <10){
-
minutes_passed = "0" + minutes_passed;
-
}
-
var seconds_passed = timeend.getSeconds();
-
if(seconds_passed <10){
-
seconds_passed = "0" + seconds_passed;
-
}
-
var milliseconds_passed = timeend.getMilliseconds();
-
if(milliseconds_passed <10){
-
milliseconds_passed = "00" + milliseconds_passed;
-
}
-
else if(milliseconds_passed <100){
-
milliseconds_passed = "0" + milliseconds_passed;
-
}
-
document.timeform.laptime.value = minutes_passed + ":" + seconds_passed + "." + milliseconds_passed;
-
}
-
}
-
-
function Stop() {
-
if(timercount) {
-
clearTimeout(timercount);
-
timercount = 0;
-
var timeend = new Date();
-
var timedifference = timeend.getTime() - timestart.getTime();
-
timeend.setTime(timedifference);
-
var minutes_passed = timeend.getMinutes();
-
if(minutes_passed <10){
-
minutes_passed = "0" + minutes_passed;
-
}
-
var seconds_passed = timeend.getSeconds();
-
if(seconds_passed <10){
-
seconds_passed = "0" + seconds_passed;
-
}
-
var milliseconds_passed = timeend.getMilliseconds();
-
if(milliseconds_passed <10){
-
milliseconds_passed = "00" + milliseconds_passed;
-
}
-
else if(milliseconds_passed <100){
-
milliseconds_passed = "0" + milliseconds_passed;
-
}
-
document.timeform.timetextarea.value = minutes_passed + ":" + seconds_passed + "." + milliseconds_passed;
-
}
-
timestart = null;
-
}
-
-
function Reset() {
-
timestart = null;
-
document.timeform.timetextarea.value = "00:00";
-
document.timeform.laptime.value = "";
-
}
-
-
//-->
-
-
</script>
and the form for events controlling
HTML:
-
<form name="timeform">
-
</form>
It's a good javascript example, including functions and "onclick" event handler.
If you need some extra explication, just leave a comment
Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
Tell me what do you think about this post. I'll write better and better entries.
11 Responses to “Javascript chronometer/stopwatch”
Leave a Reply

(1 votes, average: 4 out of 5)
Good javascript code.
I used it in order to create another similar chronometer.
Thanks!
This is good. One thing about “stop” button. It stopped when pressed. But the timer reset back to zero when pressed on “start”. This is similiar to “reset”. Can you keep the time stay the same and resume from that time when click on start button?
To Ralph. If you want to mark the times insted of pressing “stop” press the “start/lap” button then the seconds are put in the box next to the timer.
How hide that controls?
Salve.
Agradeço sua boa vontade em compartilhar seu trabalho.
Me ajudou muito.
Abraços do Brasil.
Reinaldo S Guth - reiwolf
I JUST NEED A SMALL STOP WATCH THAT TICKS VERY FAST MAYBE BLACK WITH THE DIGITAL NUMBERS BEING RED. ITS FOR MY SPACE AND CANNOT FIND AONE NO WHERE. IT WOULD BE PERFECT IF I CAN GET ONE THAT STARTS WHEN SOMEONE ENTERS MY SPACE AND KEEPS TICKING FAST UNTIL THEY LEAVE. IM NOT SURE IF YOU CAN HELP ME BUT SAW THIS PAGE AND THOUGHT I WOULD TRY. THANK YOU , FALLON
I love it! Though, as NZers are always in a hurry, I think m/seconds make timers look groovy!
I’ve been using this to do my Kegal exercises. great. thanks.
One suggestion (same as Ralph’s) when you hit stop, then start again, the time shouldn’t go back to 0. I like to pause the chronometer then resume it again leaving off at the same time.
Thanks a lot for everything you do emanuel. I have one demand please in relation with that code: could you make some kind of a timer, for exemple after 20 secondes something will happen with php ?! :s I wish I was clear :) ( English is not my language, So SORRY for mistakes ! )
Hey …
Your script is perfect… but could I ask you somehting ?
Do you think it is possible to be done that it works even after page refreshes for instance ?
Thanks a lot,
Andreea
[...] na superfície da lua. Para medir o tempo de queda dos corpos utilize um cronômetro (use este cronômetro virtual se nao possuir um real). Estime a altura que os objetos foram lançados sabendo que o astronauta [...]