.NET Zone is brought to you in partnership with:

Gil Fink, Microsoft MVP, is an expert in Web development and Microsoft data platform. He works as a senior architect at Sela Group. He is currently consulting for various enterprises and companies, where he architects and develops Web and RIA-based solutions. He conducts lectures and workshops for developers and enterprises who want to specialize in infrastructure and Web development. He is also a co-author of several Microsoft Official Courses and training kits. You can read his publications at his blog: http://blogs.microsoft.co.il/blogs/gilf. Gil is a DZone MVB and is not an employee of DZone and has posted 147 posts at DZone. You can read more from them at their website. View Full User Profile

Quick Tip – Long Polling Using jQuery

08.26.2013
| 3435 views |
  • submit to reddit

Yesterday, I helped to write infrastructure code for a JavaScript SDK API that one of my customers is building right now. One of the things that needed to be implemented as part of the SDK was the functionality of long polling. Since the SDK has a dependency on jQuery, here is the code snippet that we used as starting point to write the implementation:

function longPoll(){
    $.ajax({ 
        url: "put here the server endpoint url",
        success: function(data){
            // do whatever you want with the data you received
        },
        error: function(err) {
            // do whatever you want when error occurs
        },
        type: "GET", 
        dataType: "json", 
        complete: longPoll,
        timeout: 60000 // timeout every one minute
    });
}

In the snippet, you use the $.ajax function to start the long polling and in the complete option you use the same caller function (longPoll) to continue polling the server. Another thing to notice is the timeout option which is set to one minute.

The code is really simple and I hope it will help you the get started with long polling.

Published at DZone with permission of Gil Fink, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)