[英]Why isn't this countdown javascript working on any browser other than chrome
Safari中似乎沒有任何錯誤,並且可以在Chrome上正常運行,但這些數字並未在Safari中顯示。 我還檢查了Safari和Chrome的移動版本,但兩者均無效。 老實說,我無法理解問題所在,也許我格式化不正確,Chrome會自動對此進行補償。
代碼本身是非常基本的,只是一個簡單的函數,需要兩個日期時間並將其轉換為保存天數,小時數,分鍾數和秒數的對象。 然后,僅以1秒的間隔調用另一個函數,並將數據放入正確的元素中。
這是實時網站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 );
});
我注意到,當您將它放在firefox的控制台中時,它會帶有NaN
new TimeTill(Date.now(),'2016-04-15 20:00:00 CDT',true);
但是,在chrome中則沒有。
事實證明,代碼是正確的,顯然問題是我使用的時間戳“ Ymd H:i:s”與某些跨瀏覽器標准(稱為“符合IETF的RFC 2822時間戳”)不兼容。
基本上,我使用破折號代替斜杠,並且顯然破折號僅適用於chrome。 誰會猜到的。
只需將函數調用更改為
new TimeTill( Date.now(), new Date('2016/04/15 20:00:00 CDT'), true );
一切都很好。 謝謝大家!
作為目標日期,而不是Date.parse('2016-04-15 20:00:00 CDT')
使用類似於new Date(2016, 04, 15, 20)
Date.parse('2016-04-15 20:00:00 CDT')
東西。
然后在TimeTill
而不是var til = tar - now;
TimeTill
var til = tar - now;
使用var til = targetDate - now;
我不明白為什么根據Date.parse()文檔,您的腳本不起作用,我看不到最新瀏覽器的任何兼容性……也許與日期格式有關?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.