1

I am coding a page that makes two AJAX requests by using jQuery, each one to a different server. But the problem is when each requests needs to call to it's own timeout event. It seems that the only timeout event it's fired by the last AJAX request made. If I just do a single request on the page the timeout works, but if I add a second request the first script's timeout does not work.

I already spent hours searchig about how to fix this with out success.

Here is an example of my code to help you figure out what I am talking about:

$(document).ready(function($) { // This goes in the fist script but I could not put the script tags
    function getData() {
        $.ajax({
        url: "urlpath",
        async: false,
        dataType: "jsonp",
        timeout: 4000,  // This timeout does not work when the second request is present
            success: function(parsed_json) {
                console.log("Success for the fist request");
            },
            error: function(request, status, err) {
            if (status == "timeout") {
                console.log("Timeout error for the first request");
            } else {
                console.log("Unknown error for the fist request");
            }
            }
        });
    });
$(document).ready(function() { // This goes in the second script
    $.ajax({
    url : "urlpath.json",
    dataType : "json",
    timeout: 8000, // This timeout does work
    success: function(parsed_json) {
    console.log(Success for the last request);
    },
    error: function(request, status, err) {
        if (status == "timeout") {
            console.log(Timeout reached for the last request);
        } else {
            console.log(Unknown error on the last request);
            }
        }
    });
});

Any help is welcomed. Thank you so much in advance.

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
Ketchuz
  • 45
  • 1
  • 5
  • where are you calling `getData()`? All you show above is that it's defined within the event handler. And, since it's defined within an anonymous function, no other code can access it. – Stephen Thomas Jan 31 '14 at 21:42
  • is there probably a misunderstanding of the purpose of the timeout setting in Jquery Ajax? it's not calling any function but defines the wait Time for the response? – Axel Amthor Jan 31 '14 at 21:44
  • @AxelAmthor That's what I wanted to do, if the response takes more than x seconds to complete then fire the timeout event handler, but seems that I can have only one timeout in the page. – Ketchuz Jan 31 '14 at 22:29
  • @StephenThomas That's not the problem, I omitted the code above the `getData()` function, but the function is called on the omitted code. – Ketchuz Jan 31 '14 at 22:32

1 Answers1

0

Your first request is jsonp, not json, so you'll probably need to handle the timeout yourself: jQuery ajax (jsonp) ignores a timeout and doesn't fire the error event

Community
  • 1
  • 1
  • I don't think that's the actual problem. I have another pages just with the first request and it's timeout works as expected, and those request are made with the `JSONP` DataType. – Ketchuz Feb 01 '14 at 17:52
  • 1
    Well, I tried the code of the link you've posted and it worked! Not as complete as the real timeout event, but it is better than no error handling. Thank you so much! – Ketchuz Feb 01 '14 at 18:04