Skip to content

Instantly share code, notes, and snippets.

@endocrimes
Last active December 16, 2015 00:48
Show Gist options
  • Select an option

  • Save endocrimes/5350025 to your computer and use it in GitHub Desktop.

Select an option

Save endocrimes/5350025 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
(function () {
var Tickr = {
run: function (opts) {
var el = opts.el;
if(!el) return;
var start = Date.now(), delta;
var target = opts.target;
var duration = opts.duration;
var outputType = (el.nodeName.toLowerCase() === 'input') ? 'value' : 'innerHTML';
var currentVal = +el[outputType] || 0;
var tickr = setInterval(function () {
delta = Date.now() - start;
if(delta >= duration) {
clearInterval(tickr);
return el[outputType] = target;
}
el[outputType] = (currentVal + ((delta * (target-currentVal)) / duration)).toFixed(0);
}, 10);
}
};
window.Tickr = Tickr;
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment