Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax synchronous call with timeout

New to ajax, so asking a very basic question.

-- Is there no way to make a Synchronous ajax call (async:false) with timeout set on it.?

http://www.ajaxtoolbox.com/request/

Timeout works perfect with Asynchronous call though in my application, but for one particular scenario, I need a Synchronous call (the javascript should actually wait untill it hears back from the server), and this also works fine. But I need to handle a scenario where the sever could take long and a ajax timeout may be called.

Is there any other piece of standard documentation for ajax I could refer to?

Thanks

like image 803
Zoom Pat Avatar asked Feb 03 '10 20:02

Zoom Pat


People also ask

Does AJAX have a timeout?

Session timeout has been a very common feature in Ajax-based web applications. In responsive interface, the programmer needs to delay the ajax request to achieve some task before the response. This can be achieved by using jQuery setTimeout() function.

Can AJAX requests be made synchronous?

AJAX can access the server both synchronously and asynchronously: Synchronously, in which the script stops and waits for the server to send back a reply before continuing. Asynchronously, in which the script allows the page to continue to be processed and handles the reply if and when it arrives.

Are AJAX calls asynchronous?

AJAX's most appealing characteristic is its "asynchronous" nature, which means it can communicate with the server, exchange data, and update the page without having to refresh the page. The two major features of AJAX allow you to do the following: Make requests to the server without reloading the page.

Is AJAX call asynchronous by default?

By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false . Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.


1 Answers

Basically, during a synchronous ajax request, the browser is blocked and no javascript can be executed while the browser is blocked. Because of this, jQuery can't abort the ajax request after a set timeout because jQuery is javascript and javascript can't be executed while the browser is blocked. This is the primary flaw in synchronous ajax.

Anytime you might want a synchronous request, you should instead use an asynchronous one with what should happen afterwards in the callback, as shown below;

$.ajax({
    url : 'webservices.php',
    timeout: 200,
    dataType : 'json',
    data : {
        'cmd' : 'ping',
    },
    success : function(data, textStatus) {
        $.ajax({
            url : 'webservices.php',
            async: false,
            dataType : 'json',
            data : {
                'cmd' : 'feedback',
                'data' : data,
                'userinfo' : window.dsuser
            },
            success : function(data, textStatus) {
                // success!
                Status("Thanks for the feedback, "
                    + window.dsuser.user + "!");
            }
        });
    },
    error : function(jqhdr, textStatus,
                     errorThrown) {
        Status("There was trouble sending your feedback. Please try again later");
    }
});
like image 58
Naresh Chennuri Avatar answered Oct 11 '22 17:10

Naresh Chennuri