對於我的項目,我需要從遠處的服務器獲取一些數據, X次與AJAX。
其實它是在IE瀏覽器工作正常,但是當我嘗試在Chrome和Firefox瀏覽器的AJAX功能不起作用了$(文件)。就緒()事件的...
$(document).ready(function() {
doLaunch(); // This works
});
setInterval(function() {
if ($_dragStart == null) {
doClear(); // working but no AJAX in there
doRecup(); // not working
}
}, 10000);
function doLaunch() {
$.ajax({
type: 'POST',
url: 'journalier.aspx/copyDataYesterday',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
complete : function() {
doRecup(); // working in there
},
error: function (xhr, status, error) {
var err = eval("(" + status + ")");
alert(err);
}
});
};
function doRecup() {
$.ajax({
type: 'POST',
url: 'journalier.aspx/getDataNav',
contentType: 'application/json; charset=utf-8',
data: '{datePlanning:"' + '<%= Session["Planning_Date"].ToString() %>' + '"}',
dataType: 'json',
success: function (data) {
var dateMaj = document.getElementById("MainContent_dateTimePlanning").value;
for (i = 0; i < 1000 ; i++) {
if (data.d[(i * 5) + 3] == dateMaj) {
var objet = document.getElementById(data.d[(i * 5) + 2]);
var row = document.getElementById("MainContent_" + data.d[i * 5]);
if (row != null) {
if (data.d[(i * 5) + 1] == "Ressource")
$(objet).appendTo(row.cells[12].childNodes[0]);
else if (data.d[(i * 5) + 1] == "Conducteur")
$(objet).appendTo(row.cells[4].childNodes[0]);
else if (data.d[(i * 5) + 1] == "Chauffeur")
$(objet).appendTo(row.cells[6].childNodes[0]);
else if (data.d[(i * 5) + 1] == "Engin")
$(objet).appendTo(row.cells[5].childNodes[0]);
else if (data.d[(i * 5) + 1] == "Chef")
$(objet).appendTo(row.cells[11].childNodes[0]);
else if (data.d[(i * 5) + 1] == "Consigne" && data.d[(i * 5) + 4] != null) {
row.cells[9].childNodes[0].innerText = data.d[(i * 5) + 4];
row.cells[9].childNodes[1].value = data.d[(i * 5) + 4];
}
else if (data.d[(i * 5) + 1] == "Carburant" && data.d[(i * 5) + 4] != null) {
row.cells[3].childNodes[0].innerText = data.d[(i * 5) + 4];
row.cells[3].childNodes[1].value = data.d[(i * 5) + 4];
}
}
}
}
var currDate = new Date();
var HH = currDate.getHours();
var MM = currDate.getMinutes();
var Time = HH + ":" + MM ;
document.getElementById("MainContent_lblDateLastMaj").innerText = "Dernière Mise à Jour : " + Time;
},
error: function (xhr, status, error) {
var err = eval("(" + status + ")");
alert(err);
},
complete: function() {
alert("Complete!");
}
});
};
function doClear() {
for (k = 0; k < document.getElementById("MainContent_divTabProjet").childNodes.length/2; k++) {
for (i = 0; i < document.getElementById("MainContent_tabProjetJourAgence" + k).rows.length; i++) {
document.getElementById("MainContent_lblCarbu" + i).innerText = "";
document.getElementById("MainContent_lblCarbu" + i).value = "";
for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[4].childNodes[0].childNodes.length ; j++) {
$(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[4].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources"));
}
for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[5].childNodes[0].childNodes.length ; j++) {
$(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[5].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabEngins"));
}
for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[6].childNodes[0].childNodes.length ; j++) {
$(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[6].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources"));
}
document.getElementById("MainContent_lblConsigne" + i).innerText = "";
document.getElementById("MainContent_lblConsigne" + i).value = "";
for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[11].childNodes[0].childNodes.length ; j++) {
$(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[11].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources"));
}
for (j = 0; j < document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[12].childNodes[0].childNodes.length ; j++) {
$(document.getElementById("MainContent_tabProjetJourAgence" + k).rows[i].cells[12].childNodes[0].childNodes[j]).appendTo($("#MainContent_divTabRessources"));
}
}
}
};
更新:當我在控制檯中手動啓動功能時,它工作正常。所以問題在於函數不是在間隔中啓動,而是doClear函數每10秒啓動一次......如果我將doClear函數置於註釋中,doRecup函數每隔10秒觸發一次。
更新2:如果我這樣做,它的工作原理
setInterval(function() {
if ($_dragStart == null) {
doClear();
}
}, 10000);
setInterval(function() {
if ($_dragStart == null) {
doRecup();
}
}, 10000);
更新3:所以現在我那些:
setInterval(function() {
setTimeout(function() {
doClear();
}, 1);
setTimeout(function() {
doRecup();
}, 1);
}, 10000);
那麼它遠不是完美的,它肯定不是推薦,但實際上它的工作原理如此...
什麼不工作?你在控制檯中是否有任何錯誤? – anderssonola
@anderssonola我沒有錯誤,但doRecup函數只在ready事件中啓動,而不是每10秒啓動一次。 doClear函數正常工作。 –