"타이머가"나쁜 생각에 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);
사용이 필요합니다. setInterval(method, timespan)
을 사용하여 n 밀리 초마다 호출을 강제로 수행 할 수 있지만 서버가 백업되면 스태킹 요청을 시작할 수 있습니다 (응답 속도가 느린 네트워크에서도).
호 처리 로직의 아약스 코드에서 setTimeout(method, timespan)
을 사용하여 다음 호출을 설정하는 것이 좋습니다. (jQuery를 사용)
예 :
function getStuff()
{
$.get(
'myurl.aspx?r=' + Math.random(), // stop caching issues
function(data) {
$('#myDiv').html(data);
setTimeout(getStuff, 2000); // you might want to set this to 1900 if you need it closer to every 2 seconds
}
);
}
setTimeout(getStuff, 2000); // the initial timer initialization
감사합니다. 서버 측에서, 나는 나의 수업을 알고있다. 나는 최선을 다하겠습니다. 조금 걱정되는 것은 클라이언트 측입니다. + 페이지에 타이머를 구현하는 것에 대한 제안 사항이 있습니까? – Kamyar
자바 스크립트의 setTimeout을 사용하여 타이머를 시작한 다음 각 응답이 돌아 오면 다른 타이머를 다시 시작하는 것이 좋습니다. 'setTimeout (yourPollingFunction, 2000)'을 호출하여'yourPollingFunction'을 2 초안에 호출합니다. 대신 setInterval을 사용하려는 유혹을 피하십시오.여기에는 몇 가지 분명한 단점이 있습니다. http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx –
데이브 감사합니다. – Kamyar