1 2/* 3 * 10. jQuery.countTo 4*/ 5(function ($) { 6 'use strict'; 7 $.fn.countTo = function (options) { 8 options = options || {}; 9 10 return $(this).each(function () { 11 // set options for current element 12 var settings = $.extend({}, $.fn.countTo.defaults, { 13 from: $(this).data('from'), 14 to: $(this).data('to'), 15 speed: $(this).data('speed'), 16 refreshInterval: $(this).data('refresh-interval'), 17 decimals: $(this).data('decimals') 18 }, options); 19 20 // how many times to update the value, and how much to increment the value on each update 21 var loops = Math.ceil(settings.speed / settings.refreshInterval), 22 increment = (settings.to - settings.from) / loops; 23 24 // references & variables that will change with each update 25 var self = this, 26 $self = $(this), 27 loopCount = 0, 28 value = settings.from, 29 data = $self.data('countTo') || {}; 30 31 $self.data('countTo', data); 32 33 // if an existing interval can be found, clear it first 34 if (data.interval) { 35 clearInterval(data.interval); 36 } 37 data.interval = setInterval(updateTimer, settings.refreshInterval); 38 39 // initialize the element with the starting value 40 render(value); 41 42 function updateTimer() { 43 value += increment; 44 loopCount++; 45 46 render(value); 47
48 if (typeof(settings.onUpdate) == 'function') { 49 settings.onUpdate.call(self, value); 50 } 51 52 if (loopCount >= loops) { 53 // remove the interval 54 $self.removeData('countTo'); 55 clearInterval(data.interval); 56 value = settings.to; 57 58 if (typeof(settings.onComplete) == 'function') { 59 settings.onComplete.call(self, value); 60 } 61 } 62 } 63 64 function render(value) { 65 var formattedValue = settings.formatter.call(self, value, settings); 66 $self.text(formattedValue); 67 } 68 }); 69 }; 70 71 $.fn.countTo.defaults = { 72 from: 0, // the number the element should start at 73 to: 0, // the number the element should end at 74 speed: 1000, // how long it should take to count between the target numbers 75 refreshInterval: 100, // how often the element should be updated 76 decimals: 0, // the number of decimal places to show 77 formatter: formatter, // handler for formatting the value before rendering 78 onUpdate: null, // callback method for every time the element is updated 79 onComplete: null // callback method for when the element finishes updating 80 }; 81 82 function formatter(value, settings) { 83 return value.toFixed(settings.decimals); 84 } 85}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.