Chinaunix首页 | 论坛 | 博客
  • 博客访问: 67816
  • 博文数量: 15
  • 博客积分: 841
  • 博客等级: 准尉
  • 技术积分: 180
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 00:14
文章分类

全部博文(15)

文章存档

2011年(1)

2008年(14)

我的朋友

分类: 系统运维

2008-05-25 21:19:33

今天看到jason的一段代码用来给所有Ajax.Request设置超时时间,
但如果想给每个Ajax.request设置不同的超时间呢?我改了改(黑体部分)如下:
 
function callInProgress (xmlhttp) {
  switch (xmlhttp.readyState) {
    case 1: case 2: case 3:
      return true;
      break;
    // Case 4 and 0
    default:
      return false;
      break;
  }
}
function showFailureMessage() {
  alert('uh oh, it looks like the network is down. Try again shortly');
}
// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
  onCreate: function(request) {
    request['timeoutId'] = window.setTimeout(
     function() {
       // If we have hit the timeout and the AJAX request is active, abort it and let the user know
       if (callInProgress(request.transport)) {
       request.transport.abort();
       showFailureMessage();
       // Run the onFailure method if we set one up when creating the AJAX object
         if (request.options['onFailure']) {
           request.options['onFailure'](request.transport, request.json);
         }
       }
     }, request.options.timeoutSeconds?request.options.timeoutSeconds*1000:30*1000
    );
  },
  onComplete: function(request) {
    // Clear the timeout, the request completed ok
    window.clearTimeout(request['timeoutId']);
  }
});
像下面这样调用:
new Ajax.Request(url, {
   timeoutSeconds:5,
   parameters:{ //code... },
   onSuccess: function(transport) { //code... }
});
 
通常要想以后注销responder得这样:
var Responder = {
    onCreate: function(request){//code...},
    onComplete: function(request){//code...}
};
Ajax.Responders.register(Responder);
Ajax.Responders.unregister(Responder);
 
 
引用
阅读(3070) | 评论(0) | 转发(0) |
0

上一篇:Shell:sha-bang

下一篇:Hamachi+CCProxy+ProxyCap

给主人留下些什么吧!~~