Last active
December 24, 2015 18:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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