49
votes

I searched Stack Overflow and Google for the specific error message in the title, but I did not find it (in the context of JavaScript coding, not browser settings). On my site, there are five functions that are called every 10 seconds. These functions do things such as:

  1. Check the user's inbox for new emails
  2. Check the user's account for new instant messages
  3. etc.

However, if the user is logged in, but is INACTIVE (does not use the mouse or press any keys) for about 15 minutes, I get the following error message when I "Inspect Element" with Chrome:

Failed to load resource: net::ERR_NETWORK_IO_SUSPENDED // (x 5)
Uncaught Type Error: Cannot read property 'combinedfiletimeinput' of undefined //one of my previously defined form values is now not defined

At this point, the user's new email count, new IM count, etc. go blank (whereas they were integers before). All the user has to do is refresh the page or go to another page to reconnect, so it's not a huge deal.

My hack solution is to use a JavaScript timer to automatically logout the user if there is not any of the following events in a 15 minute period:

  1. Mouse click
  2. Mouse movement
  3. Key press

Is there a way to prevent this "Failed to load resource" error from occurring?

UPDATE: This seems to occur when the user's device sleeps/hibernates while still logged on...when the user restarts the device. This is when the error message can be seen on Chrome's Inspect Element, Firebug, etc.

UPDATE 10/02/2014: I have now condensed the five setTimeout functions into one large setTimeout function. In addition, the form that held the modify times in an input called "combinedfiletimeinput" has been removed and I now handle the file modify times differently.

Here is a screenshot of the Chrome Developer Tools log showing the error. I have added "mysite" in place of my site's name and "filename" in place of the actual filename. I have also whited out the name of the external JavaScript file, and all that remains is .js (sorry, but I'm just trying to be careful :) ) I cut off some of the screenshot, so the text would be large enough to read.

Chrome Screenshot

As you can see by the screenshot, the request processes OK for the first three requests. Then I "sleep"ed my device and then turned my device back on. That's where the next two requests are errors (in red). After these first two errors, the requests begin to process normally again (rows with black text, after rows with red text). The console clearly shows the error message.

2
I don't think you can avoid the Failed to load resource error. Have you tried to catch the error in the ajax query so that you can avoid the undefined type Error ?Kesty
@AdamFischer How does this behavior differ if you suddenly disconnect the computer from the Internet normally (e..g, pull out the Ethernet cord, turn off the WiFi card)? I understand that won't cause an ERR_NETWORK_IO_SUSPENDED, but it will cause some kind of error. I'm curious to know how an ERR_NETWORK_IO_SUSPENDED differs from a normal sudden disconnection, from the point of view of the JavaScript code. If there is no different, simply wait a few seconds and try the request again.apsillers
Did some testing, and looks like, you have to specify timeout in your ajax calls. Without timeout, the script execution stops, with timeout the script goes on.Adam Fischer
@TheOneandOnlyChemistryBlob I didn not mean function you are calling your ajax from, but ajax call itself. Checkout jQuery docs for "timeout" api.jquery.com/jQuery.ajax . Or xhr.timeout = 4000; for native javascriptAdam Fischer
@TheOneandOnlyChemistryBlob That is ok, no 404 will be returned, because request wont reach targeted server. Try adding ajaxRequest.timeout=4000 right before ajaxRequest.send()Adam Fischer

2 Answers

24
votes

Just summarizing my comments under the question for community.

After some testing, it seems, that setting Ajax timeouts for every call is a must. Without timeouts, NET:: errors such as ERR_NETWORK_IO_SUSPENDED, ERR_INTERNET_DISCONNECTED, etc will cause Ajax to stop execution, and would not resume after computer wake up.

For jQuery:

$.ajax({
   url: yourURL,
   timeout: 4000
});

For XMLHttpRequest (XHR):

 xhr.timeout = 4000;

Moreover, an exception handler is necessary for your script to catch connection errors, so that your JavaScript code would not break down because of unexpected behavior/values.

Even with timeouts and exeption handler, your application will throw NET:: errors, but they would not harm your application; you could think of them as Notices.

1
votes

As much as I think catching exceptions should be the general cure, you might try triggering fake (mouse, scroll, keyboard) events to see if that would prevent Chrome from going to "idle" status.

Stack Overflow question How to simulate a mouse click using JavaScript? might be helpful.