0

i have some code which can be found here on 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);

If you visit that Fiddle page on Chrome, Opera or Microsoft Edge you will see server/player stats auto updating every 10 seconds and working how it was intended. However if you visit on IE 11, or Firefox nothing displays in the div that is supposed to be loading the results.

I am not to good with Javascript and had some help with this code originally by someone through an IRC channel i did use JS Lint to check the code, and i updated it and fixed what they considered bad code by putting in "use strict" etc. However that did not resolve the issue on IE 11 or Firefox.

Any assistance will be greatly appreciate in figuring this out, i did use the inspector on IE 11 and Firefox and the debug console but nothing is firing or showing up. In fact, it even shows the (update) message in the browser console, but doesn't show the div!

Venugopal
  • 1,888
  • 1
  • 17
  • 30
MajorCyto
  • 15
  • 6
  • the issue is `innerText`. have a look at this http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox – PrinceG Jan 28 '16 at 04:49
  • Hi, thank you for a response. I actually found that topic shortly after my question and began reading through it. I can get the code to work properly on firefox if i use textContent but it still does not work on IE 11 even with that. So i am still trying to figure it out.. – MajorCyto Jan 28 '16 at 04:52

1 Answers1

0

Promises are not supported by IE. But there are many A+ compliant polyfills available for use instead.

innerText does not work in Mozilla Alternative attributes to choose from are textContent or innerHTML for markup.

Community
  • 1
  • 1
traktor
  • 17,588
  • 4
  • 32
  • 53
  • innerHTML seems to solve the problem for Firefox, and it still works in Chrome and Edge & Opera. But, not IE 11. Forgive me, i am not to versed in javascript in general, so i am not to sure what "Promises" refers to. Edit: Oh, i just read the first link so that's what a promise is. But, i am not versed enough to be able to re-write or use the polyfills to fix it.. But thank you for giving me the explanation link regardless. I might just have to leave IE out unfortunately. – MajorCyto Jan 28 '16 at 04:56
  • Promise objects help to avoid deeply nested call backs when multiple asynchronous operations are performed consecutively. As it stands your example could be re-written without promises by handling response data from within the load listener function. See [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest) for worked examples. – traktor Jan 28 '16 at 05:35