0

I have what should be a very simple little process. I have an interval timer which, when it expires, makes an ajax call to the server. With the json that comes back, it parses it out and updates the DOM. The setInterval is set to one second. The server then does a very simple SELECT on the database. It executes a query which takes milliseconds to execute, if that.

On our test server, it's working fine. But when it's deployed to our customer it is most of the time NOT actually hitting the database. We can see it in query analyser. There should be a continuous flow of queries, but they are sporadic at best, often 40 or more seconds between hits.

Here's the code:

setTimeout(function run() {
    // When the timer elapses, get the data from the server
    GetData(0);
    setTimeout(run, _refreshRate);
}, 1000);


function GetData(isFirstLoad) {
    //console.log("Attempting to obtain the data...");
    jQuery.ajax({
        url: "something.ashx",
        type: "GET",
        contentType: 'application/json; charset=utf-8',
        success: function(resultData) {
            //console.log("Got the data.");
            ParseJson(resultData);
            // show the last refresh date and time
            $('#refreshTime').html(GetDateTime());
        },
        error : function(xhr, textStatus, errorThrown) {
            if (textStatus == 'timeout') {
                //console.log("Timeout occured while getting data from the server.  Trying again.");
                // If a timeout happens, DON'T STOP. Just keep going forever.
                $.ajax(this);
                return;
            }
         },
         timeout: 0,
    });
}

Everything within ParseJson(resultData); works fine. And with this line...

$('#refreshTime').html(GetDateTime());

...the time gets refreshed every one second like clockwork, even though the database never gets hit.

And I can put a breakpoint in the debug tools inside the error and it never gets hit.

If we hit refresh, it works or a few seconds (we can see queries hitting the database) but then it slows way down again.

The frustrating part is that it works flawlessly on our test server. But there is clearly something I'm overlooking.

EDIT:

Ok, this is really weird. When I have debugger open, it works. As soon as I close the debugger, it stops working. I don't even have to have the network tab running and capturing events. Just the debugger window open makes it work.

This is IE, which is what the client is using so it's our only option.

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • What is `$.ajax(this);`?? Is the error callback called? Btw, if the whole thing is just fine in another deployment, you can hardly expect it to be a programming problem... – marekful Dec 10 '17 at 04:37
  • I found that on another thread here while trying to figure out how to make the ajax call simply do itself again if there is a time out. – Casey Crookston Dec 10 '17 at 04:38
  • That being said, I can set a breakpoint there both on my test server and on the client's production server, and it never gets hit. Once is set the `timeout:0` it never again timed out... which I *think* is good. – Casey Crookston Dec 10 '17 at 04:39
  • Never heard of that and I highly doubt it does that. What `this` is set to depends on the JS context so that line can cause errors or unexpected behaviour. – marekful Dec 10 '17 at 04:40
  • If request succeeds something is wrong in your back end or your analyzer. Do some server side logging – charlietfl Dec 10 '17 at 04:41
  • @marekful, ok, I appreciate that. Any recommendations? – Casey Crookston Dec 10 '17 at 04:41
  • What's the network response on the AJAX call? You can check in the developer tools of chrome in the network tab. My bet is you're getting a response, but it's an error. – Cody G Dec 10 '17 at 04:45
  • instead of `this` move all the options for first `$.ajax` and store as variable...then each `$.ajax` would look like `$.ajax(optionsObject)`. Might want a counter for timeouts also – charlietfl Dec 10 '17 at 04:45
  • @CodyG. -- Excellent suggestion! I'm going to go look. BRB – Casey Crookston Dec 10 '17 at 04:46
  • of course when you watch it, it works fine. But in the mean time, I found this: https://javascript.info/settimeout-setinterval. Using setInterval() may be the problem. – Casey Crookston Dec 10 '17 at 05:24
  • @Everyone -- see edit to OP. I am stumped. This is weird. – Casey Crookston Dec 10 '17 at 05:43
  • What version of IE?... and what jquery version – Cody G Dec 10 '17 at 05:52
  • 1
    Try to change url to this form `url: "something.ashx?"+(new Date().getTime())` – Wajih Dec 10 '17 at 05:54
  • @CodyG., I am not sure what version of IE they are running. I asked my boss that (and he's the only one who has access to their system) but never got a reply. I will press him for that. The jquery version is 3.2.1 – Casey Crookston Dec 11 '17 at 14:37

1 Answers1

2

Found the answer here:

jQuery ajax only works in IE when the IE debugger is open

Turns out iE, and only IE, will cache ajax responses. You have to tell it not to. Adding cache: false did the trick.

function GetData(isFirstLoad) {
    //console.log("Attempting to obtain the data...");
    jQuery.ajax({
        url: "something.ashx",
        type: "GET",
        contentType: 'application/json; charset=utf-8',
        cache: false,  
        success: function(resultData) {
Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • :) This won't be the last of your "ah it was the cache!" issues, believe me! – Cody G Dec 10 '17 at 06:17
  • If you care to educate me, I'm all ears!! – Casey Crookston Dec 11 '17 at 14:35
  • I already changed the way the timer is handled so that it won't overlap, but will instead wait until one task is finished before starting the next. Doing that made it so that the process never timed out anymore. (Change reflected in OP) So the only thing left (that I can tell) that might be an issue is how to handle if a timeout does occur for some reason. – Casey Crookston Dec 11 '17 at 14:51
  • 1
    Oh, I was merely stating cache issues pop up in many other variations whether it's javascript or in any programming language. Predominately the "I think it's the latest information" but "we made it use a cache so its fast so it's not" ... When it comes to debugging javascript however, Firefox and Chrome both have an option to "disable cache when developer tools are open" and I highly recommend that. – Cody G Dec 11 '17 at 19:50