0

Is there a way to timeout the request sooner when a server is unavailable? I'm requesting a file via a script tag that times out after 21 seconds at which point the page tries to build without it. The status code for the request was 502:Bad Gateway while the server was down.

I am making a request from a server that is up and running fine to the server that is temporarily in a fledgling state. The normal request looks like this:

<script language="JavaScript" src="...someFileOnTheDownServer..."></script>

When the request tries to load the file, the loading of my primary page blocks and waits on the file that won't be served.

I can load the file asynchronously but that would also appear as a 'loading' status until 21 seconds passes. I would prefer to wait only 2-5 seconds in the attempt to retrieve the file.

This question looks rather similar but I thought that after 5 years there should be better alternatives, more questions on this subject, or a real solution to timeout the <script> tag at a delay I specify.

I don't see a timeout property yet on the MDN for the script tag. Does anyone have good experience with the HTML5 async property? I would prefer to not use async indefinitely as I would prefer the experience to have this file loaded and then for the page to render...just not wait 21 seconds to do it!

Update

In order for async to be an option, there needs to be a way to only execute some page-specific javascript code as soon as the requested file was loaded.

I tested the async property and (for compatible browsers) the page renders without blocking and waiting for the file. However, I need some page-specific code to run once the page is loaded and not before. This answer seems to offer a solution but my preferred answer is in regards to being able to timeout the script loading at a specified interval.

Community
  • 1
  • 1
veeTrain
  • 2,915
  • 2
  • 26
  • 43

1 Answers1

1

Maybe you can use ajax requests.

$.holdReady( true );
function getScr (timeout) {
jQuery.ajax({
      url: "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js",
      dataType: "script",
      cache: true,
    error: function(x, t, m){
    if(t==="timeout") {
        console.log("got timeout");
    } else {
        console.log(t);
    }

      $.holdReady( false );
    },
    success: function(){
          console.log( "added" );
$('#date').text(moment().format("dddd, MMMM Do YYYY, h:mm:ss a"));
      $.holdReady( false );
    },
    timeout: timeout
})}
getScr(3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Date : <div id="date"></div>

http://api.jquery.com/jQuery.holdReady/

tkay
  • 1,716
  • 14
  • 18
  • Thank you for the idea; I have tried it out. The timeout does indeed trigger the error function after `timeout:timeout` and `t === "timeout"`. However, the script request kept chugging along and the browser continued to wait until the 21 seconds passed to stop trying. There may not be a way to forcibly cancel the request even though the error callback was reached. – veeTrain May 05 '15 at 18:18
  • I was hopeful that `x.abort()` would stop the process but it has not worked yet. – veeTrain May 05 '15 at 18:38
  • What do you mean by "However, the script request kept chugging along and the browser continued to wait until the 21 seconds passed to stop trying" ? . Request will stop on reaching the timeout , no.? check this out . http://jsfiddle.net/sg9y6c8u/ . I've disabled cache here in this example to see the changes on every click. – tkay May 05 '15 at 19:03
  • Actually, it did not. I get the same behavior as you expect with your script, but with the particular downtime configuration that our server was in today (it is in the process of being fixed) the request did NOT stop upon reaching the timeout (although the `error` function was entered). Calling `abort` did not kill it, either! For whatever reason, the browser kept trying to download the content from the unavailable server (for 21 seconds) which was 18 seconds longer than when my 3 second timeout completed. – veeTrain May 05 '15 at 19:26