1(function ($) { 2 $.fn.countTo = function (options,viewCounter) { 3 if(viewCounter == 1){ 4 options = options || {}; 5 6 return $(this).each(function () { 7 // set options for current element 8 var settings = $.extend({}, $.fn.countTo.defaults, { 9 from: $(this).data('from'), 10 to: $(this).data('to'), 11 speed: $(this).data('speed'), 12 refreshInterval: $(this).data('refresh-interval'), 13 decimals: $(this).data('decimals') 14 }, options); 15 16 // how many times to update the value, and how much to increment the value on each update 17 var loops = Math.ceil(settings.speed / settings.refreshInterval), 18 increment = (settings.to - settings.from) / loops; 19 20 // references & variables that will change with each update 21 var self = this, 22 $self = $(this), 23 loopCount = 0, 24 value = settings.from, 25 data = $self.data('countTo') || {}; 26 27 $self.data('countTo', data); 28 29 // if an existing interval can be found, clear it first 30 if (data.interval) { 31 clearInterval(data.interval); 32 } 33 data.interval = setInterval(updateTimer, settings.refreshInterval); 34 35 // initialize the element with the starting value 36 render(value); 37 38 function updateTimer() { 39 value += increment; 40 loopCount++; 41 42 render(value); 43
44 if (typeof(settings.onUpdate) == 'function') { 45 settings.onUpdate.call(self, value); 46 } 47 48 if (loopCount >= loops) { 49 // remove the interval 50 $self.removeData('countTo'); 51 clearInterval(data.interval); 52 value = settings.to; 53 54 if (typeof(settings.onComplete) == 'function') { 55 settings.onComplete.call(self, value); 56 } 57 } 58 } 59 60 function render(value) { 61 var formattedValue = settings.formatter.call(self, value, settings); 62 $self.html(formattedValue); 63 } 64 }); 65 }; 66 } 67 68 $.fn.countTo.defaults = { 69 from: 0, // the number the element should start at 70 to: 0, // the number the element should end at 71 speed: 1000, // how long it should take to count between the target numbers 72 refreshInterval: 100, // how often the element should be updated 73 decimals: 0, // the number of decimal places to show 74 formatter: formatter, // handler for formatting the value before rendering 75 onUpdate: null, // callback method for every time the element is updated 76 onComplete: null // callback method for when the element finishes updating 77 }; 78 79 function formatter(value, settings) { 80 return value.toFixed(settings.decimals); 81 } 82}(jQuery)); 83 84jQuery(function ($) { 85 // custom formatting example 86 $('.count-number').data('countToOptions', { 87 formatter: function (value, options) { 88 return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ','); 89 } 90 }); 91 92 // start all the timers 93 var viewCounter = 0; 94 $(document).on( 'scroll', function(){ 95 if($('.timer').each(count).isOnScreen() && viewCounter < 2){ 96 viewCounter++; 97 console.log(viewCounter); 98 } 99 }); 100 101function count(options) { 102 var $this = $(this); 103 options = $.extend({}, options || {}, $this.data('countToOptions') || {}); 104 $this.countTo(options,viewCounter); 105} 106}); 107 108 109// check if element visible in viewport 110$.fn.isOnScreen = function(){ 111 var win = $(window); 112 var viewport = { 113 top : win.scrollTop(), 114 left : win.scrollLeft() 115 }; 116 viewport.right = viewport.left + win.width(); 117 viewport.bottom = viewport.top + win.height(); 118 119 var bounds = this.offset(); 120 bounds.right = bounds.left + this.outerWidth(); 121 bounds.bottom = bounds.top + this.outerHeight(); 122 123 return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom)); 124 125};
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.