日期:2014-05-16 浏览次数:20640 次
上一篇只是检查是否超时,超时后却不做任何处理。这里如果超时,会给failure第二个参数msg赋值为“request timeout”。
?
这样使用者能清楚的知道一次HTTP请求的细节。实现细节较为诡异,得慢慢体会。这里timeout的效果和JQuery,Ext是一样的。如下
?
Ajax.text('../servlet/Ajax',{ timeout : 2000, success : function(result){}, failure : function(xhr,msg){ alert(msg); } });
?
?
源码见附件
xhr.onreadystatechange = function(){ if (xhr.readyState == 4 && !isTimeout){ _onStateChange(xhr, type, success, failure); clearTimeout(timer); }else{} };
xhr.onreadystatechange = function(){ if (xhr.readyState == 4 && !isTimeout){ _onStateChange(xhr, type, success, failure); clearTimeout(timer); }else{} };
if(timeout>0){ timer = setTimeout(function(){ document.write("\n<br>before abort " + xhr.readyState + isTimeout); xhr.abort(); document.write("\n<br>after abort " + xhr.readyState + isTimeout); isTimeout = true; },timeout); } xhr.onreadystatechange = function(){ document.write("\n<br>onreadystatechange " + xhr.readyState + isTimeout); if (xhr.readyState == 4 && !isTimeout){ _onStateChange(xhr, type, success, failure); clearTimeout(timer); }else{} }; before abort 1false onreadystatechange 4false after abort 0false