Javascript millisecond counter

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: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.