How long will the browser wait after an ajax request?

If you are using a jQuery $.ajax call you can set the timeout property to control the amount of time before a request returns with a timeout status. The timeout is set in milliseconds, so just set it to a very high value. You can also set it to 0 for “unlimited” but in my opinion you should just set a high value instead.

Note: unlimited is actually the default but most browsers have default timeouts that will be hit.

When an ajax call is returned due to timeout it will return with an error status of “timeout” that you can handle with a separate case if needed.

So if you want to set a timeout of 3 seconds, and handle the timeout here is an example:

$.ajax({
    url: "/your_ajax_method/",
    type: "GET",
    dataType: "json",
    timeout: 3000, //Set your timeout value in milliseconds or 0 for unlimited
    success: function(response) { alert(response); },
    error: function(jqXHR, textStatus, errorThrown) {
        if(textStatus==="timeout") {  
            alert("Call has timed out"); //Handle the timeout
        } else {
            alert("Another error was returned"); //Handle other error type
        }
    }
});​

Leave a Comment

tech