Skip to content

Instantly share code, notes, and snippets.

@ajsutton
Created November 13, 2011 19:56
Show Gist options
  • Save ajsutton/1362590 to your computer and use it in GitHub Desktop.
Save ajsutton/1362590 to your computer and use it in GitHub Desktop.
Long Poll JS Patterns
(function($) {
var longPollUrl = '/longpoll';
var lastReceivedSequence;
function poll() {
$.ajax(longPollUrl, {
data: { 'lastSequence': lastReceivedSequence },
dataType: 'json',
timeout: 15000,
success: function(data) {
if (!data) return;
lastReceivedSequence = data.sequenceNumber;
$(window).trigger('onLongPollEvent', data);
},
complete: poll
});
}
})(jQuery);
(function($) {
var longPollUrl = '/longpoll';
var lastReceivedSequence;
var serverVersion;
function poll() {
$.ajax(longPollUrl, {
data: { 'lastSequence': lastReceivedSequence },
dataType: 'json',
timeout: 15000,
success: function(data) {
if (!data) return;
lastReceivedSequence = data.sequenceNumber;
if (data.serverVersion != serverVersion) {
window.location.reload();
return;
}
$(window).trigger('onLongPollEvent', data);
},
complete: poll
});
}
})(jQuery);
$(window).bind('onLongPollEvent', function(eve, longPollData) { ... });
var serverVersion;
...
if (data.serverVersion != serverVersion) {
window.location.reload();
return;
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment