2016-04-29 107 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

謝謝,這照顧了控制檯錯誤,但我仍然收到警告消息,說它不是有效的日期。它現在也是多次彈出,而不是一次。 –

+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。