2016-04-10 13 views
1

Safariにエラーはなく、Chromeで完全に動作しています。数字はSafariには表示されません。私はSafariとChromeのモバイル版もチェックしましたが、どちらも動作していません。私は正直なところ、何が間違っているかを知ることはできません。何かを間違ってフォーマットしてしまって、Chromeがそれを自動的に補います。なぜこのカウントダウンJavaScriptはクロム以外のブラウザでも動作しません

コード自体はかなり基本的なもので、2つのdatetimeを取り、日、時間、分、秒の数を保持するオブジェクトに変換する単純な関数です。次に、もう1つの関数はちょうど1秒間隔で呼び出され、データを正しい要素に入れます。

は、ここで私はあなたがFirefoxでコンソールでこれを入れたときに、それがNaN

新しいTimeTill(思い付くことに気づいたライブサイトhttps://zeiworld.net/countdown.php

またはそれ自体によってコードhttps://jsfiddle.net/4b9py0sg/1/

$(function(){ 

    function TimeTill(startDate, targetDate, floor){ 

     //Set the starting information 
     var now = startDate; 
     var tar = Date.parse(targetDate); 
     var til = tar - now; 

     //Split the basic millieseconds between each date into days with a decimal 
     var days = til/1000/60/60/24; 

     //Take the decimal from days and multiply it by the hours in a day 
     var hours = (days % 1) * 24; 

     //Take the decimal from hours and multiply it by the minutes in an hour 
     var minutes = (hours % 1) * 60; 

     //Take the decimal from minutes and multiply it by the seconds in a minute 
     var seconds = (minutes % 1) * 60; 

     //Store each value in its respective property, flooring it if required 
     if (floor == true){ 

      this.days = Math.floor(days); 
      this.hours = Math.floor(hours); 
      this.minutes = Math.floor(minutes); 
      this.seconds = Math.floor(seconds); 

     }else{ 

      this.days = days; 
      this.hours = hours; 
      this.minutes = minutes; 
      this.seconds = seconds; 

     } 

    } 

    function updateScreen(){ 
     //Store the time between two dates in an object 
     launchIn = new TimeTill(Date.now(), '2016-04-15 20:00:00 CDT', true); 

     //Output the time parts to their respective divs 
     $('.days').html(launchIn.days); 
     $('.hours').html(launchIn.hours); 
     $('.minutes').html(launchIn.minutes); 
     $('.seconds').html(launchIn.seconds); 
    } 

    //Start and continue Loop 
    updateScreen(); 
    setInterval(updateScreen, 1000); 

}); 

答えて

1

私は「Ymd H:i:s」を使用していたタイムスタンプが「IETF準拠のRFC 2822タイムスタンプ」と呼ばれるクロスブラウザ標準に準拠していないことが、 。

基本的にはスラッシュの代わりにダッシュを使用していますが、明らかにダッシュはクロムのみで動作します。誰が推測したでしょうか?

ちょうど

new TimeTill(Date.now(), new Date('2016/04/15 20:00:00 CDT'), true); 

に関数呼び出しを変更しなければならなかった、すべてがうまくでした。皆さんありがとう!

1

ですDate.now()、 '2016-04-15 20:00:00 CDT'、true);

ただし、クロムではありません。

+0

私はfirefoxをインストールして見ています – Zei

0

Date.parse('2016-04-15 20:00:00 CDT')の代わりにnew Date(2016, 04, 15, 20)を使用してください。あなたのスクリプトはたぶんそれが関連している...私は、最新のブラウザで任意の互換性が表示されないDate.parse() documentationに応じて動作していない理由を

は、代わりにvar til = tar - now;使用のTimeTillvar til = targetDate - now;

に私は理解していません日付形式?

+0

奇妙なことに、あなたのコードはうまく動作しますが、カウントダウンはオフになります。新しい日付(2016、4、16、1)(GMTバージョンの元の時刻)毎月または何かの日数を考慮に入れてください。それは可能でしょうか? – Zei

+0

ああ私の神、それを得た。 – Zei

+0

あなたはロック! ;) – zatziky

関連する問題