I am trying to get this Jquery count-up function to work but it’s not showing up for me, can someone please show me what I am doing wrong?
I am using a function created by “mhuggins” (https://github.com/mhuggins/jquery-countTo) so I haven’t made it myself, just trying to get it to work.
countup.js
(function($) {
$.fn.countTo = function(options) {
// merge the default plugin settings with the custom options
options = $.extend({}, $.fn.countTo.defaults, options || {});
// how many times to update the value, and how much to increment the value on each update
var loops = Math.ceil(options.speed / options.refreshInterval),
increment = (options.to - options.from) / loops;
return $(this).each(function() {
var _this = this,
loopCount = 0,
value = options.from,
interval = setInterval(updateTimer, options.refreshInterval);
function updateTimer() {
value += increment;
loopCount++;
$(_this).html(value.toFixed(options.decimals));
if (typeof(options.onUpdate) == 'function') {
options.onUpdate.call(_this, value);
}
if (loopCount >= loops) {
clearInterval(interval);
value = options.to;
if (typeof(options.onComplete) == 'function') {
options.onComplete.call(_this, value);
}
}
}
});
};
$.fn.countTo.defaults = {
from: 0, // the number the element should start at
to: 100, // the number the element should end at
speed: 1000, // how long it should take to count between the target numbers
refreshInterval: 100, // how often the element should be updated
decimals: 0, // the number of decimal places to show
onUpdate: null, // callback method for every time the element is updated,
onComplete: null, // callback method for when the element finishes updating
};
})(jQuery);
And then I am trying to use it with this html page
test.html
<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.6.2.js"></script>
<script type="text/javascript" src="scripts/countup.js"></script>
<script type="text/javascript"><!--
$('.timer').countTo({from: 0, to: 500});
//--></script>
</head>
<body>
<span class="timer"></span>
</body>
</html>
When I fire up the page, nothing happens. The script is working for other people, so it’s obviously me who is doing something wrong. I am very new to this so it’s not surprising.
Thanks!
Looks like you’re missing
document.ready(function() { // timer code here });