我有一段简单的代码,我正在运行它以将计数器重复增加 1,直到达到一定数量。
该代码在 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/
谢谢。