A way to do it is to retrieve the current time using new Date().getTime()
and inside an interval compare the initial time we stored with the current time:
currentTime - startTime = difference in MS
now having that difference in MS we can easily do some basic math:
LIVE DEMO
var int, ms=0, s=0, m=0;
function swatch(){
var startTime = new Date().getTime();
int = setInterval(function(){
var time = new Date().getTime();
var dif = time-startTime;
ms= dif%1000;
s = Math.floor(dif/1000)%60;
m = Math.floor(dif/1000/60)%60;
$('#swatch').text( m+':'+s+':'+ms);
},1);
}
$(document).on('keydown', function( e ){
if(e.keyCode == 32 && !int){
swatch();
}else{
clearInterval(int);
int=0;
}
});
OR THIS WAY (Read Rick's comments below) for leading "0"s
s = ('0'+ Math.floor(dif/1000)%60 ).slice(-2);
m = ('0'+ Math.floor(dif/1000/60)%60 ).slice(-2);