Quick Tip – Long Polling Using jQuery
http://blogs.microsoft.co.il/gilf/2013/08/26/quick-tip-long-polling-using-jquery/
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:
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.
No comments:
Post a Comment