I have a javascript function that gives very different results in Chrome than it does in Safari or Firefox (it doesn't work at all in IE at the moment, but I am not worried about that).

The purpose of the function is to scan a (fairly long) text file and identify and list the 64 most frequent character pairs in that text. This involves both Ajax and regex calls.

I am using this function to scan a list of 25 text which were previously scanned with another function. The point of this exercise is to see if the two functions product identical results.

So, I use the new function to scan each text and then compare that result to a "library" of results from the older function. Safari and Firefox give identical results, but, in Chrome, the result is different.

I suspect the problem has something to do the the asynchronous character of javascript functions (and the fact that both loading and scanning the different texts require more or less time depending on the length of the text in question). I tried to cover that possibility by implementing a timing system that would make sure that the browser had time enough to finish both the Ajax call and the regex scan for each text before the next text was scanned.

It works like this:

Code:
      var index = 0;
      var round = 1;
      var limit = 4;
      var interval = 1000;
      var tryDoingIt = null;
      var doingIt = false;
      var http_request = false;

      function doIt() {
        tryDoingIt = setInterval('tryOne()', interval*round);
      }

      function tryOne() {
        //doingIt is set to true in function doOne()
        //and then set to false in function showAjaxText;
        //the idea being to allow the current text to be loaded
        //and scanned before the next one is called
        if (!doingIt) {
          if (index < titlesArray.length) {
            doOne();
          } else {
            clearInterval(tryDoingIt);
            tryDoingIt = null;
            index = 0;
            if (++round > limit) {
              alert('Done!');
            } else {
              doIt();
            }
          }
        }
      }

      function doOne() {
        doingIt = true;
        getAjaxText(titlesArray[index],'contentDiv','./texts/','ISO-8859-1');
      }

      function getAjaxText(content, target, textPath, characters) {
        http_request = false;
        // running locally on IE5.5, IE6, IE7 
        if ((location.protocol=="file:")&&(window.ActiveXObject)) {  
          if (!http_request) try { 
            http_request=new ActiveXObject("Msxml2.XMLHTTP"); 
          }catch(e){
            http_request=false;
          }
          if (!http_request) try { 
            http_request=new ActiveXObject("Microsoft.XMLHTTP"); 
          } catch(e) {
            http_request=false;
          }
        }
        // IE7, Firefox, Safari, Opera... 
        if (!http_request) try {
          http_request=new XMLHttpRequest();
        } catch(e) {
          http_request=false;
        }
        // IE6
        if (typeof ActiveXObject != "undefined") {
          if (!http_request) try {
            http_request=new ActiveXObject("MSXML2.http_request"); 
          }	catch(e) {
            http_request=false;
          }	
          if (!http_request) try {
            http_request=new ActiveXObject("Microsoft.http_request"); 
          } catch(e) {
            http_request=false;
          }	
        }		
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
          if (http_request.overrideMimeType) {  http_request.overrideMimeType('text/plain; charset=' + characters);  }
        } 
        if (!http_request) {
          alert('ERROR: Cannot create an XMLHTTP instance');
          return false;
        }
        http_request.onreadystatechange = function() { showAjaxText(content, target); }
        http_request.open('GET', textPath + content, true);
        http_request.send(null);
      }

      function showAjaxText(content, target) {
        if (http_request.readyState == 4) {
          if (http_request.status == 200 || http_request.status == 0) {
            var rDiv = document.getElementById('resultDiv'+parseInt(round));
            var resultStr = http_request.responseText;
            var pairsStr = countAllPairs(resultStr);
            pairsStr = pairsStr.substr(0,pairsStr.length - 1);
            var oldPairsStr = oldPairsArray[index];
            if (pairsStr === oldPairsStr) {
              showMatch();
            } else {
              showMismatch();
            }
            index++;
            doingIt = false;
          } else {
            //alert('Error: Code(' + httpReq.status + ')');
          }
        }
      }
The idea (in the above) is that the demo (which you can see at: www.wiseware.com/test) goes through the list of 25 titles four times, each time increasing the length argument of setInterval. Safari and Firefox are indifferent to this timing business and always produce the same result, but Chrome not only produces a different result than the other two, it also produces different results in each of the four "timing rounds".

Can anybody help me figure out what is going on here?

Thanks,

JAS

P.S. One of the texts ("The_Good_Soldier") was deliberately altered so as to guarantee at least one "mismatch" result in this trial.