we can create a javascript counter easily using serInterval() function.
here is an example of it
html for counter start and stop
<button onclick="startCounter()">Start </button> <button onclick="stopCounter()">End </button> <div class="Timer"></div>
and the javascript part is
var intervalId;
//setInterval function return a value . by passing this value we can stop it.
function startCounter(){
var start = new Date;
intr = setInterval(function() {
$('.Timer').text(((new Date - start) / 1000).toFixed(1) + " Seconds");
}, 100);
//this will update in 100 millisecond later
}
function stopCounter(){
clearInterval(intr);
}
Advertisement
Tags: counter, javascript, milisecond