我有一个令人费解的问题。一小段javascript,用于在Wordpress子主题中设置一个给定的数字,并从父主题改编而来。该脚本在我的开发环境中工作得很好,但在使用完全相同的代码库的实时环境中,代码会失败并返回熟悉的
Uncaught TypeError: undefined is not a function它似乎在第一次运行时运行良好,但在随后的运行中失败。在实时服务器上失败的特定代码行如下:
$(this).waypoint(function(direction) {我已经反复检查了代码库(它是相同的),并检查了所有文件的权限(都是可读的)。我完全被弄糊涂了--有人能解释一下吗?
你可以在这里看到http://bit.ly/1vUX8pf
在本例中,出现故障的是最上面的计数器。代码是从底部的计数器稍微改编过来的,它继续工作得很好。说我感到困惑是轻描淡写的!
完整代码如下:
(function($) {
function audioSecondsInit() {
$('.nectar-audioseconds').each(function() {
if($(this).has('[data-symbol]')) {
if($(this).attr('data-symbol-pos') == 'before') {
$(this).find('.number').prepend($(this).attr('data-symbol'));
} else {
$(this).find('.number').append($(this).attr('data-symbol'));
}
}
});
if(!$('body').hasClass('mobile')) {
$('.nectar-audioseconds').each(function() {
// FAILS HERE >>>
$(this).waypoint(function(direction) {
var $endNum = parseInt($(this).find('.number span').text());
$endNum = $endNum + (23671233 * Math.floor(( (new Date()) - Date.parse('12/01/2014') ) / 86400000));
$(this).find('.number span').countToFormat({
from: 0,
to: $endNum,
speed: 1500,
refreshInterval: 30
});
}, { offset: '105%', triggerOnce: true });
});
}
}
setTimeout(function(){
audioSecondsInit();
},100);
$.fn.countToFormat = function (options) {
console.log("countToFormat");
options = options || {};
return $(this).each(function () {
var settings = $.extend({}, $.fn.countTo.defaults, {
from: $(this).data('from'),
to: $(this).data('to'),
speed: $(this).data('speed'),
refreshInterval: $(this).data('refresh-interval'),
decimals: $(this).data('decimals')
}, options);
var loops = Math.ceil(settings.speed / settings.refreshInterval),
increment = (settings.to - settings.from) / loops;
var self = this,
$self = $(this),
loopCount = 0,
value = settings.from,
data = $self.data('countTo') || {};
$self.data('countTo', data);
if (data.interval) {
clearInterval(data.interval);
}
data.interval = setInterval(updateTimer, settings.refreshInterval);
render(value);
function updateTimer() {
value += increment;
loopCount++;
render(value);
if (typeof(settings.onUpdate) == 'function') {
settings.onUpdate.call(self, value);
}
if (loopCount >= loops) {
// remove the interval
$self.removeData('countTo');
clearInterval(data.interval);
value = settings.to;
if (typeof(settings.onComplete) == 'function') {
settings.onComplete.call(self, value);
}
}
}
function render(value) {
var formattedValue = settings.formatter.call(self, value, settings);
$self.html(numberWithCommas(formattedValue));
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
});
};
})(jQuery);发布于 2014-12-12 00:07:31
在你的网站上,我没有看到waypoint.min.js。
主页,我对它进行了调试,发现它在函数的第一次处理时出错,因为.waypoint不存在。
https://stackoverflow.com/questions/27427090
复制相似问题