在我的項目中,我使用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
任何幫助?
你可以使用正則表達式.match(/ \((? - \ d +)\)/)僅從您的消息中提取數字並將其分配給全局變量。所以在打印這個$(「#status」).html(Message)之前,你可以使用指定的全局變量來驗證它是否與以前一樣。如果不匹配,則打印,否則返回false。 –
100ms是否可能太快,並且您仍在處理同一行時正在使用該服務? – Tallmaris
@Tallmaris 100ms只是一個假設..但它取決於行的長度和數據..這就是爲什麼它stucked .. –