$.ajax timeout example wrong and hazardous?

When searched for "long-polling using jquery", this link is the top result and I'm surprised why people haven't cared to know if they were correct about what they said.

They say:

Timeout: Set a timeout (in milliseconds) for the request. The timeout period starts at the point the $.ajax call is made. Here, we set the timeout to 30 seconds. This means our poll function won't get called again until both the ajax call is complete and (at-least) thirty (30) seconds have passed.

(emphasis mine)

But that is not the case, I suppose.

I tested this code:

(function foo(){
    $.ajax({
        url:'/echo/json',
        success:function(d){
          console.log('Logged after 30secs');
        },
        timeout:30000,
        complete: foo
    });
})();

and here is a fiddle to prove that the emphasized text is wrong.

You can see that console.log() is getting called before 30 seconds.

Both the setTimeout and setInterval techniques suffer from same flaw - a new connection to the server must be opened each time the $.ajax method is called. To make that connection, your realtime app must gear up and battle through hoards of competing network traffic to make it to your server.

Is there any reliable solution(except sockets) to make long-polling flawless?

Answers


Hang on, the timeout option with jQuery's ajax method has nothing to do with long polling. The article about long polling is right. It used setTimeout in a closure. Because the next execution of the function is inside the function it self, it guarantees that the function is called one after one at a time.

The jQuery $.ajax's timeout option, however, allows specifying a time after which the call is canceled. i.e. if the server take too long to response, simply cancel the request. It has nothing to do with 'long polling'.

The block of code should be:

(function foo(){
    setTimeout(function(){
       $.ajax({
          url:'/echo/json',
          success:function(d){
            console.log('Logged after 30secs');
            foo()
          }
       });
    }, 30000);


})();

I usted this code:

(function foo(){
    $.ajax({
        url:'/echo/json',
        success:function(d){
          console.log('Logged after 30secs');
        },
        timeout:30000,
        complete: foo
    });
})();

they say: the poll function won't get called again until both the ajax call is complete and (at-least) thirty (30) seconds have passed.

That is indeed very wrong. It should be: "the poll function will get called as soon as either the ajax call is complete or (at most) thirty (30) seconds have passed" for your code.

However, notice that neither this code nor that statement can be found on their website. They use a code that is very different from yours:

(function poll() {
   setTimeout(function() {
       $.ajax({ url: "server", success: function(data) {
            sales.setValue(data.value);
       }, dataType: "json", complete: poll });
    }, 30000);
})();

Here the poll function will get called again as soon as thirty seconds have passed and then the ajax call is completed.

Both the setTimeout and setInterval techniques suffer from same flaw - a new connection to the server must be opened each time the $.ajax method is called. To make that connection, your realtime app must gear up and battle through hoards of competing network traffic to make it to your server.

Is there any reliable solution (except sockets) to make long-polling flawless?

Well, opening a new connection lies in the nature of the technique. You cannot really prevent that (but notice that the network can optimize this). However, imho the focus of the article was on the flaw that you were going to have a huge lot of requests if they are immediately responded to by the server ("steady" polling). You can counter this flaw by using long polling (changing how the server treats poll requests):

Long polling addresses the weakness of traditional polling by keeping the connection to your server open. Keeping the connection to the server open eliminates the travel time from client to server and thus, significantly reduces the issues surrounding network latency.


Need Your Help

How to use input string as variable call in python?

python variables python-3.x

I am a beginner python learner. I was wondering if it was possible to create a basic massage encrypting system without using any modules. I want my programm to check and use variable name that is in

Changing Placeholder Text On Focus

jquery html html5 input placeholder

Is there a simple solution for changing placeholder text when an input is in focus? For example:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.