Skip to content

Instantly share code, notes, and snippets.

@mekegi
Created April 15, 2014 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mekegi/10715061 to your computer and use it in GitHub Desktop.
Save mekegi/10715061 to your computer and use it in GitHub Desktop.
/**
* @since 04.10.13 11:56
* @author Arsen Abdusalamov
* @param {} $ jQuery
* @param {} window
* @returns void
*/
(function ($, window) {
"use strict";
window.CometClient = function () {
var me = this,
requestCount = 0,
lastData = {};
this.longPolling = function (url, successCallback) {
requestCount++;
$.ajax({
url: url + (requestCount === 1 ? '&first=1' : ''),
success: function (data) {
if (JSON.stringify(lastData) !== JSON.stringify(data)) {
setTimeout(function () {
successCallback(data);
}, 1000);
lastData = data;
console.log('not equal');
} else {
console.log('equal');
}
setTimeout(function () {
me.longPolling(url, successCallback);
}, 1500);
},
error: function () {
setTimeout(function () {
me.longPolling(url, successCallback);
}, 5000);
},
global: false
});
};
};
})(window.jQuery, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment