diff --git a/dist/jquery.longpoll.min.js b/dist/jquery.longpoll.min.js index 6d62393..a15afb0 100644 --- a/dist/jquery.longpoll.min.js +++ b/dist/jquery.longpoll.min.js @@ -4,4 +4,4 @@ * @license https://opensource.org/licenses/BSD-3-Clause * @author Viktor Khokhryakov */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){"use strict";function b(b,c){function e(){i=a.ajax({type:f.type,url:f.url,data:g,dataType:"json"}).done(function(b){var c=!1;a.each(b.params,function(a,b){if(g[a]!=b)return c=!0,!1}),g=b.params,c&&a.isFunction(j.callback)&&j.callback.call(j,b.data),h=setTimeout(e,f.pollInterval)}).fail(function(a){a.status&&(h=setTimeout(e,f.pollErrorInterval))}).always(function(){i=null})}var f=a.extend({},d,c||{});if(!a.isPlainObject(f.params)||a.isEmptyObject(f.params))throw new Error('Invalid "params" property');var g=f.params,h=null,i=null,j=this;this.callback=f.callback,this.getId=function(){return b},this.isActive=function(){return null!==i||null!==h},this.start=function(){j.isActive()||e()},this.stop=function(){null!==i&&i.abort(),null!==h&&(clearTimeout(h),h=null)}}a.longpoll={get:function(a){return c[a]},register:function(a,d){if("string"!=typeof a)throw new Error('The "id" must be a string');return void 0!==c[a]&&c[a].stop(),c[a]=new b(a,d),c[a]},destroy:function(a){void 0!==c[a]&&(c[a].stop(),delete c[a])}};var c={},d={type:"GET",url:void 0,params:void 0,callback:void 0,pollInterval:500,pollErrorInterval:5e3}}); \ No newline at end of file +!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):a("object"==typeof exports?require("jquery"):jQuery)}(function(a){"use strict";function b(b,c){function e(){i=a.ajax({type:f.type,url:f.url,data:g,dataType:"json"}).done(function(b){a.each(b.params,function(c,d){if(g[c]!==d)return g=b.params,a.isFunction(j.callback)&&j.callback.call(j,b.data),!1}),h=setTimeout(e,f.pollInterval)}).fail(function(a){a.status&&(h=setTimeout(e,f.pollErrorInterval))}).always(function(){i=null})}var f=a.extend({},d,c||{});if(!a.isPlainObject(f.params)||a.isEmptyObject(f.params))throw new Error('Invalid "params" property');var g=f.params,h=null,i=null,j=this;this.callback=f.callback,this.getId=function(){return b},this.isActive=function(){return null!==i||null!==h},this.start=function(){j.isActive()||e()},this.stop=function(){null!==i&&i.abort(),null!==h&&(clearTimeout(h),h=null)}}a.longpoll={get:function(a){return c[a]},register:function(a,d){if("string"!=typeof a)throw new Error('The "id" must be a string');return void 0!==c[a]&&c[a].stop(),c[a]=new b(a,d),c[a]},destroy:function(a){void 0!==c[a]&&(c[a].stop(),delete c[a])}};var c={},d={type:"GET",url:void 0,params:void 0,callback:void 0,pollInterval:500,pollErrorInterval:5e3}}); \ No newline at end of file diff --git a/package.json b/package.json index 0d76661..da5ae5f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery-longpoll-client", - "version": "0.2.1", + "version": "0.3.0", "description": "Simple client-side longpoll implementation", "keywords": ["jquery", "ajax", "longpoll", "comet"], "main": "src/jquery.longpoll.js", diff --git a/src/jquery.longpoll.js b/src/jquery.longpoll.js index 5c0f0fa..4c6f403 100644 --- a/src/jquery.longpoll.js +++ b/src/jquery.longpoll.js @@ -91,17 +91,15 @@ data: params, dataType: "json" }).done(function (response) { - var triggered = false; $.each(response.params, function (id, value) { - if (params[id] != value) { - triggered = true; + if (params[id] !== value) { + params = response.params; + if ($.isFunction(self.callback)) { + self.callback.call(self, response.data); + } return false; } }); - params = response.params; - if (triggered && $.isFunction(self.callback)) { - self.callback.call(self, response.data); - } timeoutId = setTimeout(doLoop, settings.pollInterval); }).fail(function (e) { if (e.status) {