我使用 Web 服务,因为它们要快得多。但是如果你使用 UpdatePanels,Webservices 是没用的。此外,我还要说,您不应该每 x 秒更新一次页面,但首先要询问是否有更新要做。这节省了很多;-)
这可能是一个小例子,我没有尝试过,但曾经这样工作过。是ms ajax版本,需要scriptmanager
Type.registerNamespace("myproject");
myproject.updateControl = function () {
myproject.updateControl.initializeBase(this);
this._xhr = null;
this._updating = false;
this._timer = null;
}
myproject.updateControl.prototype = {
initialize: function () {
myproject.updateControl.callBaseMethod(this, 'initialize');
this.startTimer();
},
startTimer: function () {
if (this._timer) clearTimeout(this._timer);
this._timer = setInterval(Function.createDelegate(this, this._timerWork), 2000);
},
stopTimer: function () {
clearTimeout(this._timer);
this._timer = null;
},
_timerWork: function () {
if (this._updating || !this._checkXhr()) return;
this._xhr = Sys.Net.WebServiceProxy.invoke("myServicePath Or null if PageMethod", "checkForUpdate",
false,
null,
Function.createDelegate(this, this._onCheckedUpdate));
},
_onCheckedUpdate: function (data) {
this._xhr = null;
if (data.needsUpdate) {
this._update();
}
},
_udpate: function () {
if (!this._checkXhr) return;
this._updating = true;
this._xhr = Sys.Net.WebServiceProxy.invoke("servicepath", "updateMe", false, { param: param }, Function.createDelegate(this, this._updateSucces));
},
_updateSuccess: function (data) {
alert("yeah i´m get updated");
this._updating = false
this._xhr = null;
},
_checkXhr: function () {
if (this._xhr()) {
if (confirm("There is an active call to the Server. If you wait to long, it may have been broken. Do you want to Abort the current call?")) {
this._xhr.get_executor().abort();
this._xhr = null;
return true;
} else {
return false;
}
}
return true;
},
dispose: function () {
myproject.updateControl.callBaseMethod(this, 'dispose');
}
}
myproject.updateControl.registerClass('myproject.updateControl', Sys.Component);
用法
$create(myproject.updateControl);
或者
var upd = new myproject.updateControl();
upd.initialize();