2

У меня есть простой кусок кода, который я запускаю, чтобы увеличивать счетчик на 1 до тех пор, пока он не достигнет определенной суммы.jQuery Counter Функция, не работающая в IE <8

Код работает нормально на Chrome/FF/IE9 +, но на IE7/8 он достигает 1,80 фунта стерлингов и прекращает работу.

Кажется, я не могу найти его, но он, очевидно, работает определенное количество раз, а затем останавливается.

Основная функция:

 (function($) { 
      $.fn.countTo = function(options) { 
       options = $.extend({}, $.fn.countTo.defaults, options || {}); 
       var loops = Math.ceil(options.speed/options.refresh_interval), 
        increment = (options.to - options.from)/loops; 
       return $(this).each(function() { 
        var _this = this, 
         loop_count = 0, 
         value = options.from, 
         interval = setInterval(update_timer, options.refresh_interval); 
        function update_timer() { 
         value += increment; 
         loop_count++; 
         if(options.format == 'money') { 
          $(_this).html('\u00A3' + number_with_commas(value.toFixed(options.decimals))); 
         } else { 
          $(_this).html(value.toFixed(options.decimals)); 
         } 
         if(typeof(options.on_update) == 'function') { 
          options.on_update.call(_this, value); 
         } 
         if(loop_count >= loops) { 
          clearInterval(interval); 
          value = options.to; 
          if(typeof(options.on_complete) == 'function') { 
           options.on_complete.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 
       refresh_interval: 100, // how often the element should be updated 
       decimals: 2, // the number of decimal places to show 
       on_update: null, // callback method for every time the element is updated, 
       on_complete: null // callback method for when the element finishes updating 
      }; 
     })(jQuery); 

Пример является: http://jsfiddle.net/yxEaN/

Благодаря.

+2

любая ошибка в консоли? –

+0

Извините, забыли упомянуть - никаких ошибок в консоли – Ryan

+0

Может быть, это один из ваших обратных вызовов? – cfs

ответ

1

Ваша проблема решена для IE. Проверьте мой ниже код

function number_with_commas(x) { 
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
} 

$.ajaxSetup ({ 
    // Disable caching of AJAX responses */ 
    cache: false 
}); 

(function($) 
{ 
$.fn.countTo = function(options) { 
    options = $.extend({}, $.fn.countTo.defaults, options || {}); 
    var loops = Math.ceil(options.speed/options.refresh_interval), 
     increment = (options.to - options.from)/loops; 
    return $(this).each(function() { 
     var _this = this, 
      loop_count = 0, 
      value = options.from, 
      interval = setInterval(function(){ 
       value += increment; 
       loop_count++; 
       if(options.format == 'money') { 
        $(_this).html('\u00A3' + number_with_commas(value.toFixed(options.decimals))); 
       } else { 
        $(_this).html(value.toFixed(options.decimals)); 
       } 
       if(typeof(options.on_update) == 'function') { 
        options.on_update.call(_this, value); 
       } 
       if(loop_count >= loops) { 
        clearInterval(interval); 
        value = options.to; 
        if(typeof(options.on_complete) == 'function') { 
         options.on_complete.call(_this, value); 
        } 
       } 

       },100); 

    }); 
}; 

$.fn.countTo.defaults = { 
    from: 0, 
    to: 100, 
    speed: 1000, 
    refresh_interval: 100, 
    decimals: 2, 
    on_update: null, 
    on_complete: null 
}; 
})(jQuery); 

$(function($) { 
    $('#total_charity_counter').countTo({ 
    format: 'money', 
    from: 0, 
    to: 4229.01, 
    speed: (4229.01/180), 
    refresh_interval: .01, 
    on_complete: function(value) { 
    $(this).hide().fadeIn(1000); 
} 
    }); 
}); 

 Смежные вопросы

  • Нет связанных вопросов^_^