AJAX and setTimeout in Internet Explorer

I am trying to get a datetime server using ajax. I had a problem running a script in Internet Explorer.


My old code was like that. (But it only displays the client PC datetime, which can be changed by the client at any time)

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 + "");

Conclusion: Wednesday, December 02, 2015 11:47 (GMT + 8)

So, I switched to AJAX and made a PHP page.

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')).")";

?>

Conclusion: Wednesday, December 02, 2015 11:47 (GMT + 0800)

+4
source share
1 answer

For me, this seems like a problem with ajax call caching, because the request URL will be the same all the time. You may try

$.ajaxSetup({
    cache: false
});

, . , .

+3

Source: https://habr.com/ru/post/1618227/


All Articles