我有一段简单的代码,我正在运行以反复递增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/
感谢。
答案 0 :(得分: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);
}
});
});