1(function ($) { 2 'use strict'; 3 4 $.fn.countTo = function (options) { 5 options = options || {}; 6 7 return this.each(function () { 8 // Set options for current element 9 var settings = $.extend({}, $.fn.countTo.defaults, { 10 from: $(this).data('from'), 11 to: $(this).data('to'), 12 speed: $(this).data('speed'), 13 refreshInterval: $(this).data('refresh-interval'), 14 decimals: $(this).data('decimals') 15 }, options); 16 17 // How many times to update the value, and how much to increment the value on each update 18 var loops = Math.ceil(settings.speed / settings.refreshInterval), 19 increment = (settings.to - settings.from) / loops; 20 21 // References & variables that will change with each update 22 var self = this, 23 $self = $(this), 24 loopCount = 0, 25 value = settings.from, 26 data = $self.data('countTo') || {}; 27 28 $self.data('countTo', data); 29 30 // If an existing interval can be found, clear it first 31 if (data.interval) { 32 clearInterval(data.interval); 33 } 34 data.interval = setInterval(updateTimer, settings.refreshInterval); 35 36 // Initialize the element with the starting value 37 render(value); 38 39 function updateTimer() { 40 value += increment; 41 loopCount++; 42 43 render(value); 44 45 if (typeof settings.onUpdate === 'function') { 46 settings.onUpdate.call(self, value); 47 } 48 49 if (loopCount >= loops) { 50 // Remove the interval 51 $self.removeData('countTo'); 52 clearInterval(data.interval); 53 value = settings.to; 54 55 render(value); 56 57 if (typeof settings.onComplete === 'function') { 58 settings.onComplete.call(self, value); 59 } 60 } 61 } 62 63 function render(value) { 64 var formattedValue = settings.formatter.call(self, value, settings); 65 66 if (settings.addCommas) { 67 formattedValue = addCommas(formattedValue, settings); 68 } 69 $self.text(formattedValue); 70 } 71 }); 72 }; 73 74 $.fn.countTo.defaults = { 75 from: 0, // The number the element should start at 76 to: 0, // The number the element should end at 77 speed: 1000, // How long it should take to count between the target numbers 78 refreshInterval: 100, // How often the element should be updated 79 decimals: 0, // The number of decimal places to show 80 formatter: formatter, // Handler for formatting the value before rendering 81 onUpdate: null, // Callback method for every time the element is updated 82 onComplete: null, // Callback method for when the element finishes updating 83 addCommas: true, 84 separator: ',' 85 }; 86 87 function addCommas(value, settings) { 88 var x, x1, x2, rgx; 89 value += ''; 90 x = value.split('.'); 91 x1 = x[0]; 92 x2 = x.length > 1 ? '.' + x[1] : ''; 93 rgx = /(\d+)(\d{3})/; 94 while (rgx.test(x1)) { 95 x1 = x1.replace(rgx, '$1' + settings.separator + '$2'); 96 } 97 return x1 + x2; 98 } 99 100 function formatter(value, settings) { 101 return value.toFixed(settings.decimals); 102 } 103}(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.