var Qs_KeepSession = qs.createObject(); Qs_KeepSession.prototype = { _interval: 300, _url: 'keep-session', _timestamp: 0, _timer: null, _obj: null, initialize: function (options) { if (typeof options == 'object') { if (typeof options.interval != 'undefined') { this.setInterval(options.interval); } if (typeof options.url != 'undefined') { this.setUrl(options.url); } } var d = new Date(); this._timestamp = d.getTime(); }, setInterval: function (interval) { interval = parseInt(interval); if (!isNaN(interval) && interval != 0) { this._interval = interval * 1000; return true; } alert('keepSession: time interval is wrong!'); return false; }, getInterval: function () { return this._interval; }, setUrl: function(url) { this._url = '' + url; return true; }, getUrl: function () { return this._url; }, start: function () { if (this._timer != null) { this.stop(); } this.sendRequest(); return true; }, stop: function () { if (this._timer != null) { clearTimeout(this._timer); } this._timer = null; return true; }, initTimer: function () { this._timer = setTimeout($.proxy(this.sendRequest, this), this.getInterval()); }, sendRequest: function () { $.ajax({ url: this._url + '?' + this._timestamp, type: 'GET' }).always($.proxy(this.onResponse, this)); this._timestamp++; return true; }, onResponse: function (xhr, textStatus) { if (textStatus == 'success') { this.initTimer(); } } };