ajax 組件自動放棄和延時控制下載

來源:互聯網
上載者:User

<script>
/*
著作權:偉大的SB作者是群27521907的SB群豬
以上!刪除者是豬!

大概功能,執行某個組試下載,
如果多個同組執行下載,會最後執行最後一個請求.
並且對不同時間的請求進行放棄和延時控制下載.
*/
window.XmlHttp = {}
//建立XML對象
window.XmlHttp.XMLObject = function(){
 var HaObject = null
 if ( HaObject == null ) try { var HaObject = new XMLHttpRequest(); } catch(Err) {}
 if ( HaObject == null ) try { var HaObject = new ActiveXObject("Microsoft.XMLHTTP"); } catch(Err) {}
 if ( HaObject == null ) try { var HaObject = new ActiveXObject("Msxml2.XMLHTTP"); } catch(Err) {}
 return HaObject
}
//斷開XMLHTTP 的TCP串連,部分遊覽器或者控制項無效
window.XmlHttp.XMLClose = function(DaObject){
 try { if ( DaObject != null ) DaObject.abort(); } catch(Err) {}
 try { if ( DaObject != null ) DaObject.close(); } catch(Err) {}
}

window.XmlHttp.Data = {}
window.XmlHttp.Create = function(DaData){
 if ( XmlHttp.Data[DaData.Group] != null ) {
  //如果這個在50毫秒內激發兩個相同組的載入,那麼這個載入使用當前載入,並延時100毫秒
  if ( XmlHttp.Data[DaData.Group].Status == 0 && XmlHttp.Data[DaData.Group].Timer <= new Date().getTime() ) {
   XmlHttp.Data[DaData.Group].Timer = new Date().getTime() + 100;
  }
  //如果這個組正在下載資料,但是卻超過了10秒,那麼判斷逾時,重新下載
  if ( XmlHttp.Data[DaData.Group].Status == 1 && XmlHttp.Data[DaData.Group].Timer < new Date().getTime() - 10000 ) {
   XmlHttp.Data[DaData.Group].LoadBout = (XmlHttp.Data[DaData.Group].LoadBout || 0) + 1
   //這個組載入逾時三次,那麼不再繼續
   if ( XmlHttp.Data[DaData.Group].LoadBout < 3 ) {
    XmlHttp.Data[DaData.Group].Status = 0
    XmlHttp.Data[DaData.Group].Timer = new Date().getTime();
   }else{
    XmlHttp.XMLClose(XmlHttp.Data[DaData.Group].Object);
    delete XmlHttp.Data[DaData.Group];
   }
  }
  //正在下載,就接受了更改地址的命令,記錄資料,等待當前下載完畢後重新發出命令
  if ( XmlHttp.Data[DaData.Group].Status == 1 && XmlHttp.Data[DaData.Group].Timer <= new Date().getTime() ) {
   XmlHttp.Data[DaData.Group].NewData = DaData;
  }
  //這個組在上一次載入完畢後,在1000毫秒內又發生一次載入,那麼延長500毫秒再載入,否則延長100毫秒再載入
  if ( XmlHttp.Data[DaData.Group].Status == 2 ) {
   if ( XmlHttp.Data[DaData.Group].Timer < new Date().getTime() - 1000 ) {
    XmlHttp.Data[DaData.Group].Status = 0
    XmlHttp.Data[DaData.Group].Timer = new Date().getTime() + 100;
   }else{
    XmlHttp.Data[DaData.Group].Status = 0
    XmlHttp.Data[DaData.Group].Timer = new Date().getTime() + 500;
   }
  }
 }
 XmlHttp.Data[DaData.Group] = XmlHttp.Data[DaData.Group] || {
  Status : 0,
  Timer : new Date().getTime() + 100
 };
 XmlHttp.Data[DaData.Group].Data = DaData
 
 XmlHttp.Interval = XmlHttp.Interval || window.setInterval(
  function(){
   var IsNull = true;
   for (var Data in XmlHttp.Data){
    IsNull = false;
    if ( XmlHttp.Data[Data].Timer <= new Date().getTime() ) {
     if ( XmlHttp.Data[Data].Status == 0 ) {
      XmlHttp.XMLClose(XmlHttp.Data[Data].Object);
      try {
       XmlHttp.Data[Data].Object = XmlHttp.XMLObject();
       XmlHttp.Data[Data].Object.open("GET", XmlHttp.Data[Data].Data.Address)
       XmlHttp.Data[Data].Object.send("")
       XmlHttp.Data[Data].Status = 1
       
       document.getElementById("dada").innerHTML = parseInt(document.getElementById("dada").innerHTML) + 1
      } catch(Err) {
       XmlHttp.Data[Data].Status = 3
      }
     }
     if ( XmlHttp.Data[Data].Status == 1 ) {
      if ( XmlHttp.Data[Data].Object.readyState == 4 ){
       var HaStatus = XmlHttp.Data[Data].Object.status
       if ( HaStatus == 200 || HaStatus == 0 ){
        XmlHttp.Data[Data].Content = XmlHttp.Data[Data].Object.responseText
        try { XmlHttp.Data[Data].Data.onLoad(XmlHttp.Data[Data]); } catch(Err) {
         XmlHttp.Data[Data].Status = 3
        }
       }
       XmlHttp.XMLClose(XmlHttp.Data[Data].Object);
       XmlHttp.Data[Data].Object = null;
       XmlHttp.Data[Data].Status = 2
       if ( XmlHttp.Data[Data].NewData != null ) {
        XmlHttp.Create(XmlHttp.Data[Data].NewData);
        delete XmlHttp.Data[Data].NewData;
       }
      }
     }
    }
    //載入完畢後,如果在2000豪秒內都沒有發生新的載入,清除變數
    if ( XmlHttp.Data[Data] != null && XmlHttp.Data[Data].Status == 2 && XmlHttp.Data[Data].Timer < new Date().getTime() - 2000 ) {
     XmlHttp.XMLClose(XmlHttp.Data[Data].Object);
     delete XmlHttp.Data[Data];
    }
    //發生錯誤,等待三秒後清除變數
    if ( XmlHttp.Data[Data] != null && XmlHttp.Data[Data].Status == 3 && XmlHttp.Data[Data].Timer < new Date().getTime() - 3000 ) {
     XmlHttp.XMLClose(XmlHttp.Data[Data].Object);
     delete XmlHttp.Data[Data];
    }
   }
   //如果已經沒有任何一個載入正在繼續,刪除線程。
   if ( IsNull == true ) {
    window.clearTimeout(XmlHttp.Interval)
    delete XmlHttp.Interval;
   }
  }
 , 50)
}

 

 


function ABC(DaThis){
 document.getElementById("ttt1").innerHTML = DaThis.value
 XmlHttp.Create(
  {
   Group : "ABC",//組
   Address : "http://bbs./post.php?action=newthread&fid=1&previewpost=%D4%A4%C0%C0%CC%FB%D7%D3&subject=" + DaThis.value,//地址
   onLoad : function(DaData){//載入完備執行
    document.getElementById("div1").innerHTML = ( DaData.Content.match(/(size="45" value=")([^"]+)(" tabindex="3")/i) || {} )[2].replace(/(^size="45" value=")(" tabindex="3"$)/i, "")
   }
  }
 )
}
function DET(DaThis){
 document.getElementById("ttt2").innerHTML = DaThis.value
 XmlHttp.Create(
  {
   Group : "DET",//組
   Address : "http://bbs./post.php?action=newthread&fid=1&previewpost=%D4%A4%C0%C0%CC%FB%D7%D3&subject=" + DaThis.value,//地址
   onLoad : function(DaData){//載入完備執行
    document.getElementById("div2").innerHTML = ( DaData.Content.match(/(size="45" value=")([^"]+)(" tabindex="3")/i) || {} )[2].replace(/(^size="45" value=")(" tabindex="3"$)/i, "")
   }
  }
 )
}

</script>

<div>XMLHTTP open number:<font id="dada">0</font></div><br>


<div id="ttt1" style="width:100%; border:#999999 1px solid; padding:0px; margin:0px; font-size:12px;">a</div>
<div id="div1" style="width:100%; border:#999999 1px solid; padding:0px; margin:0px; font-size:12px;">a</div>
<input style="width:100%; border:#999999 1px solid; padding:0px; margin:0px; font-size:12px;background:#D8E2F5" value="a" onkeypress="ABC(this)" onkeyup="ABC(this)" onchange="ABC(this)">

<div id="ttt2" style="width:100%; border:#999999 1px solid; padding:0px; margin:0px; font-size:12px;">a</div>
<div id="div2" style="width:100%; border:#999999 1px solid; padding:0px; margin:0px; font-size:12px;">a</div>
<input style="width:100%; border:#999999 1px solid; padding:0px; margin:0px; font-size:12px;background:#D8E2F5" value="a" onkeypress="DET(this)" onkeyup="DET(this)" onchange="DET(this)">

相關文章

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。