53

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    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 Oct 01 '14 at 14:22
  • 2
    @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 Oct 01 '14 at 15:10
  • @apsillers, are you posing this question to me or Kesty? I have never tried it the way you suggested...I could give it a try – The One and Only ChemistryBlob Oct 01 '14 at 15:14
  • @TheOneandOnlyChemistryBlob I suppose I'm posing it to you and to the user who has offered a bounty on your question (who I'm guessing has the same problem). – apsillers Oct 01 '14 at 15:15
  • @Kesty...no not yet but it's a good idea......will try to use it to reload any content that disappears upon this error (yes, page content sometimes disappears when this error occurs) – The One and Only ChemistryBlob Oct 02 '14 at 03:16
  • @apsillers while disconected I recieve net::ERR_INTERNET_DISCONNECTED , but there is no difference in behavior. Script execution just stops there and I am unable to call ajax until refresh. – Adam Fischer Oct 02 '14 at 07:48
  • 1
    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 Oct 02 '14 at 08:14
  • can you provide a test link or make same thing happen in jsfiddle? i believe this scenario is happening: one of those 5 ajax functions that overwrite critical values. – Mohammad Hossein Amri Oct 02 '14 at 09:39
  • @Adam Fischer, on my site the Ajax calls are from setTimeout, not setInterval – The One and Only ChemistryBlob Oct 02 '14 at 12:12
  • 1
    @TheOneandOnlyChemistryBlob I didn not mean function you are calling your ajax from, but ajax call itself. Checkout jQuery docs for "timeout" http://api.jquery.com/jQuery.ajax/ . Or xhr.timeout = 4000; for native javascript – Adam Fischer Oct 02 '14 at 12:19
  • @Adam Fischer, my site uses old fashioned ajax and this can't be changed....I tried to callback an alert on 404 using: if ((ajaxRequest.readyState === 4) && (ajaxRequest.status === 404)) { alert('hello'); } but no alert is called – The One and Only ChemistryBlob Oct 02 '14 at 12:39
  • @Adam Fischer I am attaching a screenshot of how the error appears on Chrome Developer Tools...I have whited out my site's name and the actual filename and inserted "mysite" and "filename" – The One and Only ChemistryBlob Oct 02 '14 at 12:46
  • 1
    @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 Oct 02 '14 at 13:01
  • `Uncaught Type Error: Cannot read property 'combinedfiletimeinput' of undefined //one of my previously defined form values is now not defined` - Your comment is not valid, the whole object isn't defined at that point! – GuyT Oct 02 '14 at 13:06
  • @Adam Fischer...just tried ajaxRequest.timeout=4000; before send() and repeated my conditions listed above (in post update) exactly the same....same result/error...doesn't appear anything changed – The One and Only ChemistryBlob Oct 02 '14 at 13:06
  • @GuyT....that was a form value that was updated on the callback to track the modify times of all the relevant files....it would go empty after the error occurred and then on the next ajax request was undefined...I have since removed this form – The One and Only ChemistryBlob Oct 02 '14 at 13:08
  • @GuyT...I'll add the info from my last comment to my update...thanks for mentioning this – The One and Only ChemistryBlob Oct 02 '14 at 13:10
  • @TheOneandOnlyChemistryBlob That error is OK (it is expected), the important thing is, that script execution continues after computer wakes from sleep. – Adam Fischer Oct 02 '14 at 13:26
  • @Adam Fischer...I goofed...I added ajaxRequest.timeout=4000; before send for the wrong ajax function...I added this for the correct function :) and the same error message appears but now it just occurs once instead of twice (twice without the timeout you suggested). I increased it to 12000 from 4000 but no difference – The One and Only ChemistryBlob Oct 02 '14 at 13:30
  • @Adam Fischer...FYI I cannot reliably reproduce twice versus once regarding the error....all I can reliably reproduce is the error – The One and Only ChemistryBlob Oct 02 '14 at 13:39
  • 1
    @TheOneandOnlyChemistryBlob Do you have error handler? Try if (ajaxRequest.status === 200) { // your code } else { console.log("Error", ajaxRequest.statusText); } Keep the timeout on 4000. Than you should still see error msg, but it should not make any problem to you application. Than just forget about the error :) – Adam Fischer Oct 02 '14 at 15:55
  • @Adam Fischer...I tried what you suggested (tweaked one thing....changed the if else to: if (status !== 200) { console.log("Error", ajaxRequest.statusText); } but I kept the timeout...Identical error message still appears....also tried it with the if else....still get the error message – The One and Only ChemistryBlob Oct 02 '14 at 18:23
  • You will get the error msg, it just wont do you any harm now. Or? – Adam Fischer Oct 03 '14 at 07:32
  • @Adam Fischer....I'm not sure yet..I have to thoroughly check to make sure no html containers go blank (as it has occurred for my site when this error occurs)....thanks for your suggestions and help – The One and Only ChemistryBlob Oct 07 '14 at 15:23

2 Answers2

27

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.

Adam Fischer
  • 1,075
  • 11
  • 23
1

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.

Community
  • 1
  • 1
ptrk
  • 1,800
  • 1
  • 15
  • 24
  • 1
    @ptrk...how about if the user intentionally hibernates or sleeps his/her device? Your solution works for preventing device from sleeping due to inactivity – The One and Only ChemistryBlob Oct 02 '14 at 15:00
  • Agreed.In the case of hibernation, if a page reload is all the user has to do, then opening a hidden iframe with the same URL and then closing it should do as well. You only need to catch the exception and automate what the user does now manually. – ptrk Oct 05 '14 at 14:01
  • 4
    Forcing the system to stay awake is generally bad practice. On battery powered devices such as laptops & tablets, it would needlessly burn battery. It's also goes against most OS app guidelines, though a web page blurs the line. – pmont Mar 15 '16 at 15:21