Skip to content

Instantly share code, notes, and snippets.

@dmtintner
Last active December 24, 2015 18:39
Show Gist options
  • Save dmtintner/6844509 to your computer and use it in GitHub Desktop.
Save dmtintner/6844509 to your computer and use it in GitHub Desktop.
A counter for displaying a formatted number based off of the current date in milliseconds and animating its change
Counter = function(){
var n = Math.round((Date.now() - 1380990000000)/1000),
nFormatted = addCommasToNumber(n),
delay = 10;
$('#number').html(nFormatted);
setInterval(function(){
var finish = Math.round((Date.now() - 1380990000000)/1000),
currentVal = finish-delay;
var i = setInterval(function(){
if (currentVal === finish) clearInterval(i);
nFormatted = addCommasToNumber(currentVal);
currentVal++;
$('#number').html(nFormatted);
}, delay*10);
}, delay*1000);
function addCommasToNumber(x){
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment