当所有其他浏览器正确显示时,为什么IE显示NaN

时间:2018-07-19 03:26:09

标签: javascript date internet-explorer math

我正在使用倒数计时器脚本。我已经能够在Google Chrome,Microsoft Edge,Mozilla Firefox上成功获得所需的结果,但是当我转到Microsoft Internet Explorer 11时,结果的分钟和秒的倒数显示为NaN。

这是我正在使用的脚本

canonical import path

基本上,脚本会比较当前日期和截止日期,并产生两者之间的时间差。从那里开始,根据结果显示倒计时。就像我到目前为止希望所有浏览器(不包括IE)所希望的那样,所有作品都可以帮助我找出原因。

编辑

当我回显$ query-> Cutoff;日期/时间结果为yyyy-mm-dd hh:mm:ss

在浏览器中查看代码时,它显示为

 <script>
 var today = new Date();
 var DD = today.getDate();
 var MM = today.getMonth()+1; //January is 0!
 var YYYY = today.getFullYear();
 //let get the Difference in Sec btw the two dates
 var _DateFromDBProgEndDate = '<?php echo $query->Cutoff; ?>';
 var ProgEndTime = new Date(_DateFromDBProgEndDate);
 var TodayTime = new Date();

 var differenceTravel = ProgEndTime.getTime()- TodayTime.getTime() ;
 var seconds = Math.floor((differenceTravel) / (1000));

 var SecDiffFromToday = seconds;
 var seconds = SecDiffFromToday;
 function timer() {
     var days        = Math.floor(seconds/24/60/60);
     var hoursLeft   = Math.floor((seconds) - (days*86400));
     var hours       = Math.floor(hoursLeft/3600);
     var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
     var minutes     = Math.floor(minutesLeft/60);
     var remainingSeconds = seconds % 60;
     if (remainingSeconds < 10) {
         remainingSeconds = "0" + remainingSeconds; 
     }
     if (minutes < 10) {
       minutes = "0" + minutes; 
     }
     document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds + " REMAINING";

    if (minutes > 15) {

        document.getElementById('countdown').innerHTML = "";
    }
if (hours > 0) {

    document.getElementById('countdown').innerHTML = "";
}

if (seconds == 0) {
    clearInterval(countdownTimer);
    document.getElementById('countdown').innerHTML = "CLOSED";
} else {
    seconds--;
  }
 }
 var countdownTimer = setInterval('timer()', 1000);
 </script>

更新

借助社区的建议,我得以将其分类,并使其在包括可怕的IE哈哈在内的所有浏览器中都能正常工作。这是我修改后的代码,我试图评论更改的位置,以便更好地为其他人描述解决方案。

这是最后的剧本

 <script>
 var today = new Date();
 var DD = today.getDate();
 var MM = today.getMonth()+1; //January is 0!
 var YYYY = today.getFullYear();
 //let get the Difference in Sec btw the two dates
 var _DateFromDBProgEndDate = '2018-07-18 23:15:00';
 var ProgEndTime = new Date(_DateFromDBProgEndDate);
 var TodayTime = new Date();

 var differenceTravel = ProgEndTime.getTime()- TodayTime.getTime() ;
 var seconds = Math.floor((differenceTravel) / (1000));

 var SecDiffFromToday = seconds;
 var seconds = SecDiffFromToday;
 function timer() {
    var days        = Math.floor(seconds/24/60/60);
    var hoursLeft   = Math.floor((seconds) - (days*86400));
    var hours       = Math.floor(hoursLeft/3600);
    var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
    var minutes     = Math.floor(minutesLeft/60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds; 
    }
    if (minutes < 10) {
        minutes = "0" + minutes; 
    }
     document.getElementById('countdown').innerHTML = "minutes + ":" + remainingSeconds + " REMAINING";

    if (minutes > 15) {

        document.getElementById('countdown').innerHTML = "";
    }
    if (hours > 0) {

        document.getElementById('countdown').innerHTML = "";
    }

    if (seconds == 0) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "CLOSED";
    } else {
        seconds--;
    }
 }
 var countdownTimer = setInterval('timer()', 1000);
 </script>

所有协助并指出正确方向的人都有很多义务。

1 个答案:

答案 0 :(得分:-1)

借助社区的建议,我得以将其分类,并使其在包括可怕的IE哈哈在内的所有浏览器中都能正常工作。这是我修改后的代码,我试图评论更改的位置,以便更好地为其他人描述解决方案。

这是最后的剧本

<script>
 var today = new Date();
 var DD = today.getDate();
 var MM = today.getMonth()+1; //January is 0!
 var YYYY = today.getFullYear();
 //let get the Difference in Sec btw the two dates
 // UPDATED HERE
 // I have updated the echo to control the output of the date/time. by doing this i can better control the output and equally successfully appease the IE Gods.
 var _DateFromDBProgEndDate = '<?php echo  date('Y/m/d H:i:s', strtotime($query->Cutoff)); ?>'; 
 var ProgEndTime = new Date(_DateFromDBProgEndDate);
 var TodayTime = new Date();

 var differenceTravel = ProgEndTime.getTime()- TodayTime.getTime() ;
 var seconds = Math.floor((differenceTravel) / (1000));

 var SecDiffFromToday = seconds;
 var seconds = SecDiffFromToday;
 function timer() {
     var days        = Math.floor(seconds/24/60/60);
     var hoursLeft   = Math.floor((seconds) - (days*86400));
     var hours       = Math.floor(hoursLeft/3600);
     var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
     var minutes     = Math.floor(minutesLeft/60);
     var remainingSeconds = seconds % 60;
     if (remainingSeconds < 10) {
         remainingSeconds = "0" + remainingSeconds; 
     }
     if (minutes < 10) {
       minutes = "0" + minutes; 
     }
     document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds + " REMAINING";

    if (minutes > 15) {

        document.getElementById('countdown').innerHTML = "";
    }
if (hours > 0) {

    document.getElementById('countdown').innerHTML = "";
}

if (seconds == 0) {
    clearInterval(countdownTimer);
    document.getElementById('countdown').innerHTML = "CLOSED";
} else {
    seconds--;
  }
 }
 var countdownTimer = setInterval('timer()', 1000);
 </script>

或者,由于之前的代码将利用客户端时间,因此我还通过添加一些php变量来更新了脚本,以包括服务器时间,如下所示。

  // added in the date values via php in order to get the server time as opposed to the client times for a more accurate countdown.
<?php
    date_default_timezone_set('America/Toronto');
    $currentdate =  date("o/n/N");
    $currentday = date("N");
    $currentmonth = date("n");
    $currentyear = date("o");
    $currenttime= date("Y/m/d H:i:s");
?>
<script>
var today = <?php echo $currentdate; ?>;
var DD = <?php echo $currentday; ?>;
var MM = <?php echo $currentmonth; ?>; //January is 0!
var YYYY = <?php echo $currentyear; ?>;
//let get the Difference in Sec btw the two dates
var _DateFromDBProgEndDate = '<?php echo date('Y/m/d H:i:s', strtotime($inpersonresult->BidCutoff)); ?>';
var ProgEndTime = new Date(_DateFromDBProgEndDate);
var _TodayTime = '<?php echo date('Y/m/d H:i:s', strtotime($currenttime)); ?>';
var TimeOfToday = new Date(_TodayTime);

var differenceTravel = ProgEndTime.getTime()- TimeOfToday.getTime() ;
var seconds = Math.floor((differenceTravel) / (1000));

var SecDiffFromToday = seconds;
var seconds = SecDiffFromToday;
function timer() {
    var days        = Math.floor(seconds/24/60/60);
    var hoursLeft   = Math.floor((seconds) - (days*86400));
    var hours       = Math.floor(hoursLeft/3600);
    var minutesLeft = Math.floor((hoursLeft) - (hours*3600));
    var minutes     = Math.floor(minutesLeft/60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds; 
    }
    if (minutes < 10) {
        minutes = "0" + minutes; 
    }
     document.getElementById('countdown').innerHTML = "WARNING:SUBMISSIONS MUST BE RECEIVED BEFORE <?php echo date('l,F j,Y h:i a', strtotime($query->Cutoff)); ?><br>"+ minutes + ":" + remainingSeconds + " REMAINING";

    if (minutes >= 15) {

        document.getElementById('countdown').innerHTML = "";
    }
        if (hours > 0) {

        document.getElementById('countdown').innerHTML = "";
    }

    if (seconds == 0) {
         clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "CLOSED";
        window.location='https://example.com/closed/';
    } else {
        seconds--;
    }
}
var countdownTimer = setInterval('timer()', 1000);
</script>

我尝试使用所有ISO-8601接受的格式,但是这样做确实发现了一些不利结果,因此将CurrentTime和数据库生成的日期设置为Y / m / d。我在IE中发现的最大问题是,他们不喜欢在日期格式中看到连字符,这似乎会对结果产生负面影响。当我删除连字符并再次添加斜杠时,此脚本可在所有4种浏览器以及android和iphone设备上正常使用。

我还在计时器用尽时添加了重定向,该重定向将它们从页面移到另一个窗口,您可以在其中保存您选择的错误消息。