var Qs_KeepSession = qs.create(); 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; }, sendRequest: function () { var obj = this; $.ajax({ url: obj._url + '?' + obj._timestamp, type: 'GET', complete: function(XMLHttpRequest, textStatus) { if (textStatus == 'success') { obj._timer = setTimeout(function(){obj.sendRequest()}, obj._interval); } } }); obj._timestamp++; return true; } }