在Internet Explorer中使用AJAX和setTimeout

4
我正在尝试使用ajax获取服务器日期时间。在Internet Explorer上运行脚本时遇到了问题。
我的旧代码是这样的。(但它只显示客户端PC的日期时间,这可以随时由客户端更改)
var _current = new Date();
var _day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][_current.getDay()];
var _month = ["January","February","March","April","May","June","July","August","September","October","November","December"][_current.getMonth()]
var _date = _current.getDate(); if(_date < 10){_date = "0" + _date;}
var _year = _current.getFullYear();
var _hours = _current.getHours(); if(_hours > 12){_hours = _hours - 12; var ampm = "PM"}else{var ampm = "AM"}if(_hours < 10){_hours = "0" + _hours;}
var _minutes = _current.getMinutes(); if(_minutes < 10){_minutes = "0" + _minutes;}
var _seconds = _current.getSeconds(); if(_seconds < 10){_seconds = "0" + _seconds;}
$("#datetime").html("");
$("#datetime").html(_day + ", " + _month + " " + _date + ", " + _year + ", " + _hours + ":" + _minutes + ":" + _seconds + " " + ampm + "");

输出: 2015年12月02日 星期三 凌晨11点47分 (GMT+8)

因此,我转换为使用AJAX并创建了一个PHP页面。

function getDateTime(){
    $.ajax({
        url: 'datetime.php',
        success:function(content){
            $("#datetime").html("");
            $("#datetime").append(content);
        }
    });
    window.setTimeout(getDateTime,1000);
}

php

<?php

// Set Timezone
date_default_timezone_set('Asia/Taipei');

// Display DateTime
echo date("l, F d, Y, h:i:s A",strtotime('Now'))."(GMT".date("O",strtotime('Now')).")";

?>

输出:2015年12月02日周三上午11:47(GMT+0800)


你遇到了什么错误? - vijayP
IE的版本是什么?它在我的IE10上工作。 - Ash
@vijayP - 它只显示了datetime一次。它没有每秒更新。 :( - ThatGuy
@Ash - Internet Explorer 11 - ThatGuy
只需查看它是否适用于您?https://jsfiddle.net/tsttm987/ - Ash
1个回答

3

在我看来,这似乎是与ajax调用的缓存有关的问题,因为请求URL始终相同。你可以尝试

$.ajaxSetup({
    cache: false
});

这将添加一些随机请求查询字符串,并且如果有任何缓存问题,它将会解决。或许值得一试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接