2013-05-28 18 views
0

在我的項目中,我使用Setinterval來顯示Excel數據庫的導入狀態。 即,如果excelsheet包含100條記錄意味着我的狀態消息, 行1個0F 100插入的行 行2 100的插入的行..如何在javascript中使用setinterval來顯示導入任務的狀態?

但是,如果行包含任何空值意味着我需要得到錯誤詳細信息並在狀態消息中添加。所以它適用於一些問題。

我的javascript功能如下。

function ImportFormSuccess(taskId) { 
     endform(); 
    Tid = taskId; 

      try { 
       intervalId = setInterval(function() { 
        $.ajax(
        { 
         type: "POST", 
         url: rootDir + "Import/Progress", 
         data: { id: Tid }, 
         success: function (data) { 

          if (data.split(',').length > 1) { 
           ErrorMessage = ErrorMessage + data.split(',')[1] + "<br/>"; 
           updateMonitor(); 
          } 
          else { 
           Message = data; 
           updateStatus(); 
          } 
         } 
        }); 
       }, 100); 

      } 
      catch (err) { 
       txt = "Error Description" + err.Message + "</br>"; 
       txt += "Click Ok to Continue. . ."; 
       alert(txt); 
      } 

     function updateMonitor() { 
      $('#monitors').attr("class", ""); 
      $("#monitors").html(Message + "<br/>" + ErrorMessage); 
     } 

     function updateStatus() { 
      $('#status').attr("class", ""); 
      $("#status").html(Message); 

     } 

查看代碼是:

<div id="monitors" style="padding: 2px;width:500px;height:150px;overflow:auto;border:1px solid;></div> 

如果我運行該項目,並開始導入手段, 它顯示以下內容:

Row 5 Insert Failed. Row Contains Empty Value 
Row 5 Insert Failed. Row Contains Empty Value 
Row 5 Insert Failed. Row Contains Empty Value 
Row 5 Insert Failed. Row Contains Empty Value 
Row 7 Insert Failed. Row Contains Empty Value 
Row 7 Insert Failed. Row Contains Empty Value 
Row 7 Insert Failed. Row Contains Empty Value 
Row 7 Insert Failed. Row Contains Empty Value 
Row 10 Insert Failed. Row Contains Empty Value 
Row 10 Insert Failed. Row Contains Empty Value 
Row 10 Insert Failed. Row Contains Empty Value 
Row 10 Insert Failed. Row Contains Empty Value 
Row 15 Insert Failed. Row Contains Empty Value 
Row 15 Insert Failed. Row Contains Empty Value 
Row 15 Insert Failed. Row Contains Empty Value 

但我需要它只能打印一次.. 即,

Row 5 Insert Failed. Row Contains Empty Value 
Row 7 Insert Failed. Row Contains Empty Value 
Row 10 Insert Failed. Row Contains Empty Value 
Row 15 Insert Failed. Row Contains Empty Value 

任何幫助?

+0

你可以使用正則表達式.match(/ \((? - \ d +)\)/)僅從您的消息中提取數字並將其分配給全局變量。所以在打印這個$(「#status」).html(Message)之前,你可以使用指定的全局變量來驗證它是否與以前一樣。如果不匹配,則打印,否則返回false。 –

+0

100ms是否可能太快,並且您仍在處理同一行時正在使用該服務? – Tallmaris

+0

@Tallmaris 100ms只是一個假設..但它取決於行的長度和數據..這就是爲什麼它stucked .. –

回答

0

最後我解決了這個問題。

我用匹配函數檢查thar errormessage是否已經存在或不存在?

if (data.split(',').length > 1) { 
    if (!ErrorMessage.match(data.split(',')[1])) { 
      $("#monitors").show(); 
      ErrorMessage = ErrorMessage + data.split(',')[1] + "<br/>"; 
      updateMonitor(); 
      } 
    } 

感謝所有誰是幫了我很多關於我的問題..特別感謝@ Anna.P,@TallMaris,@VitorCanova

1
globalVar = ""; 

Message = data; 
         var rowVal = data.match(/((-?\d+))/)[1]; 

         if(globalVar == "") 
         { 
          globalVar = rowVal; 
          updateStatus(); 
         } 
         else 
         { 
          if(globalVar != rowVal) 
          { 
           updateStatus(); 
          } 
         } 

嘗試像這樣

+0

您的代碼也不會爲我工作.. –

+0

你在這個代碼中得到任何錯誤 –

+0

沒有錯誤@ Anna.P ..預期的輸出不會來 –

0

從評論之後,我會嘗試這樣的事:

function ImportFormSuccess(taskId) { 
    endform(); 
    Tid = taskId; 

    try { 
     callServer(); 
    } 
    catch (err) { 
     // ...snip... 
    } 
} 

function callServer() { 
    $.ajax({ 
     type: "POST", 
     url: rootDir + "Import/Progress", 
     data: { id: Tid }, 
     success: function (data) { 
      // your code here 
     }, 
     complete: function() { 
      // if not 100% (or something similar) 
      setTimeout(function() { callServer(); }, 100) 
     } 
    }); 
} 
+0

亞讓我現在檢查它.. –

+0

代碼沒有工作@Tallmaris –

相關問題