文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Ajax.Request 设置超时时间

Ajax.Request 设置超时时间

时间:2008-05-25  来源:Aowken

今天看到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);     引用http://codejanitor.com/wp/2006/03/23/ajax-timeouts-with-prototype/
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载