我有一些代码可以在 JS Fiddle 上找到
var getJSON = function (url) {
"use strict";
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};
function updateCount() {
"use strict";
getJSON('http://192.99.124.166:8080/count').then(function(data) {
console.log('update');
stats.innerText = data.result; //display the result in an HTML element
});
}
updateCount();
setInterval(updateCount, 10000);
如果您在 Chrome、Opera 或 Microsoft Edge 上访问该 Fiddle 页面,您将看到服务器/播放器统计信息每 10 秒自动更新一次,并且按预期工作。但是,如果您在 IE 11 或 Firefox 上访问,本应加载结果的 div 中不会显示任何内容。
我不擅长使用 Javascript,最初有人通过 IRC channel 对这段代码提供了一些帮助,我确实使用 JS Lint 来检查代码,我更新了它并修复了他们认为错误的代码,方法是放入“使用strict”等。然而,这并没有解决 IE 11 或 Firefox 上的问题。
如果您能提供帮助,我们将不胜感激,我确实在 IE 11 和 Firefox 上使用了检查器,并使用了调试控制台,但没有触发或显示任何内容。事实上,它甚至在浏览器控制台中显示(更新)消息,但不显示 div!
最佳答案
promise 是 not supported by IE .但是有很多A+ compliant polyfills可供使用。
innerText
does not work in Mozilla可供选择的替代属性是用于标记的 textContent
或 innerHTML
。
关于Javascript 适用于 Chrome & Opera & Edge,但不适用于 FireFox 或 IE 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052925/