私は、関数を介して一意の ID とタイマーを使用して新しい div をページに添付するフォームによって日付が渡されるプロジェクトを作成しています。このプロジェクトは Chrome では完全に機能しますが、IE の Firefox では機能しません。問題はIDです。countdown() 関数で id アラートを受け取りますが、コードで指定されているカウントダウン プラグイン内では受け取りません。興味深いことに、"id1"、"id2"、"id3" のような一意の ID を持つ複数の div ではなく、"id" のような ID を持つ div が 1 つしかない場合、プラグインは Firefox でも機能します。問題は、Firefox がカウントダウン プラグイン内の複数の ID を何らかの形で解析していないことです。countdown(id) 関数を参照してください。
function add_countdown_handler() {
$('.countdownParentDiv').each(function () {
var btn = this;
countdown(btn.id);
});
}
function countdown(id) {
var date = $('#end_date'+id).val(); //eg- 2016-01-01 00:00:00
alert(id); **//working**
alert(date); **//working**
$('#countdown'+id).countdown({ date: date}, function() {
alert('id:'+id); **//not working**
alert('date:'+d); **//not working**
});
}
//これは正常に動作するカウントダウン プラグインです
(function($) {
$.fn.countdown = function (options,callback) {
var settings = {'date': null};
if(options) {
$.extend(settings, options);
}
var this_sel = $(this);
function count_exec() {
eventDate = Date.parse(settings['date']) / 1000;
currentDate = Math.floor($.now() / 1000);
if(eventDate <= currentDate) {
callback.call(this);
clearInterval(interval);
}
seconds = eventDate-currentDate;
days = Math.floor(seconds/(60*60*24));
seconds -= days*60*60*24;
hours = Math.floor(seconds/(60*60));
seconds -= hours * 60 * 60;
minutes = Math.floor(seconds/60);
seconds -= minutes * 60;
days = (String(days).length !==2) ? '0' + days : days;
hours = (String(hours).length !==2) ? '0' + hours : hours;
minutes = (String(minutes).length !==2) ? '0' + minutes : minutes;
seconds = (String(seconds).length !==2) ? '0' + seconds : seconds;
if(!isNaN(eventDate)) {
this_sel.find('.days').text(days);
this_sel.find('.hours').text(hours);
this_sel.find('.mins').text(minutes);
this_sel.find('.secs').text(seconds);
}
}
count_exec();
var interval = setInterval(count_exec, 1000);
}
})(jQuery);
これは完璧なChromeで得られるものです。ここでタイマーが機能しています:
これは同じ写真ですが、Firefoxから切り取ったものです(タイマーは 00:00:00 です... )