2016-04-29 22 views
0

ChromeまたはFirefoxで表示しているときに期待通りに機能しているカウントダウンタイマーがあります。ただし、Safariで表示するとエラーが発生します。JavascriptのカウントダウンタイマーがSafariで動作しない

ここでは、コードです:

!function(e) { 
    e.fn.countdown = function(t, n) { 
     function s() { 
      if (eventDate = Date.parse(o.date)/1e3, currentDate = Math.floor(e.now()/1e3), currentDate >= eventDate) { 
       if ("undefined" === n || null == n) 
        return !1; 
       n.call(this), clearInterval(interval) 
      } 
      seconds = eventDate - currentDate, 
       days = Math.floor(seconds/86400), 
       seconds -= 60 * days * 60 * 24, 
       hours = Math.floor(seconds/3600), 
       seconds -= 60 * hours * 60, 
       minutes = Math.floor(seconds/60), 
       seconds -= 60 * minutes, 
       a.find(".timeRefDays").text(1 == days ? "day" : "days"), 
       a.find(".timeRefHours").text(1 == hours ? "hour" : "hours"), 
       a.find(".timeRefMinutes").text(1 == minutes ? "minute" : "minutes"), 
       a.find(".timeRefSeconds").text(1 == seconds ? "second" : "seconds"), 
       "on" == o.format && (days = String(days).length >= 2 ? days : "0" + days, hours = String(hours).length >= 2 ? hours : "0" + hours, minutes = String(minutes).length >= 2 ? minutes : "0" + minutes, seconds = String(seconds).length >= 2 ? seconds : "0" + seconds), 
       isNaN(eventDate) ? (alert("Invalid date. Here's an example: 12 Tuesday 2015 17:30:00"), clearInterval(interval)) : (a.find("> li .days").text(days), a.find("> li .hours").text(hours), a.find("> li .minutes").text(minutes), a.find("> li .seconds").text(seconds)) 
     } 
     var a = jQuery(this), o = { 
      date: null, 
      format: null 
     }; 
     t && e.extend(o, t), s(), interval = setInterval(s, 1e3) 
    } 

Safariのコンソールにエラーメッセージがある: 「変数が見つかりません:間隔」これはSafariで

働いていないのはなぜか?

+0

'interval = setInterval(s、1e3)'を割り当てる前に 's()'を呼び出しています。 – Barmar

答えて

0

interval変数の宣言を関数の先頭に追加します。

!function(e) { 
e.fn.countdown = function(t, n) { 
    var interval; 
    function s() { 
     if (eventDate = Date.parse(o.date)/1e3, currentDate = Math.floor(e.now()/1e3), currentDate >= eventDate) { 
      if ("undefined" === n || null == n) 
       return !1; 
      n.call(this), clearInterval(interval) 
     } 
     seconds = eventDate - currentDate, days = Math.floor(seconds/86400), seconds -= 60 * days * 60 * 24, hours = Math.floor(seconds/3600), seconds -= 60 * hours * 60, minutes = Math.floor(seconds/60), seconds -= 60 * minutes, a.find(".timeRefDays").text(1 == days ? "day" : "days"), a.find(".timeRefHours").text(1 == hours ? "hour" : "hours"), a.find(".timeRefMinutes").text(1 == minutes ? "minute" : "minutes"), a.find(".timeRefSeconds").text(1 == seconds ? "second" : "seconds"), "on" == o.format && (days = String(days).length >= 2 ? days : "0" + days, hours = String(hours).length >= 2 ? hours : "0" + hours, minutes = String(minutes).length >= 2 ? minutes : "0" + minutes, seconds = String(seconds).length >= 2 ? seconds : "0" + seconds), isNaN(eventDate) ? (alert("Invalid date. Here's an example: 12 Tuesday 2015 17:30:00"), clearInterval(interval)) : (a.find("> li .days").text(days), a.find("> li .hours").text(hours), a.find("> li .minutes").text(minutes), a.find("> li .seconds").text(seconds)) 
    } 
    var a = jQuery(this), o = { 
     date: null, 
     format: null 
    }; 
    t && e.extend(o, t), s(), interval = setInterval(s, 1e3) 
} 
+0

ありがとう、それはコンソールのエラーの世話をしましたが、私はまだそれが有効な日付ではないという警告メッセージが表示されています。ただ1つではなく、複数回ポップアップしています。 –

+0

'o.date'の値は何ですか?異なるブラウザでは、解析時に異なる日付形式がサポートされており、Safariが理解できる形式ではない可能性があります。 – Barmar

1
function initCountDown() 
    { 
     if ('countdown' in $.fn) { 
     $('.time-left .time').each(function() { 
     var timer = this; 
     if ($(timer).data('countdownInstance') == undefined) { 
     $(timer).countdown($(timer).data('time'), 
     function(event) { 
      $(timer).find('.days .count').text(event.offset.totalDays); 
      $(timer).find('.hours .count').text(event.offset.hours); 
      $(timer).find('.minutes .count').text(event.offset.minutes); 
      $(timer).find('.seconds .count').text(event.offset.seconds); 
         }); 
       }    
      });   
    }   
} 

これは私のコードです。 FireFoxとChormeでは問題ありませんが、Safariでは動作しません。

関連する問題