2016-09-16 133 views
0

我有兩個.asp腳本 - 第一個將sys.tables和sys.views的內容複製到SQL中的另一個表。第二屆腳本計算(從newtable的/(COUNT(從SYS.TABLES從sys.views計數())+ COUNT(*)))完成百分比在等待Ajax響應時調用循環的javascript函數

* 100

我有一個功能調用第一個腳本。

function importTablesAndViews() 
{ 
    xmlhttpImportTablesAndViews=GetXmlHttpObject(); 

    if (xmlhttpImportTablesAndViews==null) 
    { 
     alert ("Your browser does not support XMLHTTP!"); 
     return; 
    } 
    url="importtablesandviews.asp"; 
    url=url+"?sid="+Math.random(); 

    xmlhttpImportTablesAndViews.onreadystatechange=function(){if (xmlhttpImportTablesAndViews.readyState==4){ 
     var aspResponse = xmlhttpImportTablesAndViews.responseText; 
    alert(aspResponse); }} 
    xmlhttpImportTablesAndViews.open("GET",url,true); 
    xmlhttpImportTablesAndViews.send(null); 
} 

我有另一個函數來調用進度腳本

function refreshImportProgress() 
{ 
    //alert('Refresh...'); 
    xmlhttpRefreshImportProgress=GetXmlHttpObject(); 

    if (xmlhttpRefreshImportProgress==null) 
    { 
     alert ("Your browser does not support XMLHTTP!"); 
     return; 
    } 
    url="importtablesandviewsprogress.asp"; 
    url=url+"?sid="+Math.random(); 

    xmlhttpRefreshImportProgress.onreadystatechange=function(){if (xmlhttpRefreshImportProgress.readyState==4){ 
     var aspResponse = xmlhttpRefreshImportProgress.responseText; document.getElementById('progressDiv').innerHTML = aspResponse; }} 
    xmlhttpRefreshImportProgress.open("GET",url,true); 
    xmlhttpRefreshImportProgress.send(null); 
} 

我希望能夠做的是呼籲3秒的間隔refreshImportProgress()函數從importTablesAndViews內什麼()函數。

這可能嗎?我曾嘗試使用谷歌同步阿賈克斯請求,但迄今沒有多少運氣。

回答

0

onreadystatechange處理器的xmlhttpRefreshImportProgressrefreshImportProgress啓動一個定時器(setTimeout)(只要進度低於100%):

function refreshImportProgress() { 
    // ... 

    xmlhttpRefreshImportProgress.onreadystatechange = function() { 
     if (xmlhttpRefreshImportProgress.readyState == 4) { 
      document.getElementById('progressDiv').innerHTML = xmlhttpRefreshImportProgress.responseText; 

      var progress = parseInt(xmlhttpRefreshImportProgress.responseText, 10); 
         // ↑ this assumes the value of responseText is a integer only or a integer with a percentage symbol (44%) 

      if (progress < 100) { 
       setTimeout(refreshImportProgress, 3000); 
      } 
     } 
    } 

    // ... 
} 

而且在importTablesAndViews()就叫refreshImportProgress發送請求後:

function importTablesAndViews() { 
    // ... 
    xmlhttpImportTablesAndViews.send(null); 

    refreshImportProgress(); 
} 
0

道歉,如果我誤解了這個問題。 但我不確定在3秒計時器上如何處理Ajax是最好的方法。例如,如果您的瀏覽器非常慢,則無法保證第一種方法的更改能夠及時完成。另外3秒鐘等待快速瀏覽器可能是不必要的。

如果你必須這樣做,那麼只需要調用的setTimeout() http://www.w3schools.com/jsref/met_win_settimeout.asp

它似乎更容易,你應該實施每當需要時間的問題都解決了被稱爲承諾。 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

甚至是一個事件監聽器。這裏有一份清單。 http://www.w3schools.com/jsref/dom_obj_event.asp

不應該真的需要使用超時,除非你只是想延遲異步的事情,直到下一個滴答聲。