你好,我有一个名为FindNewAlertes()的函数,其中有for循环,对于每次迭代,如果经过的时间少于5秒,我都会调用另一个函数。 这是源代码
let Alertes = [];
function GetAlertes() {
$.ajax({
url: "/Observer/GetDynamicAlarmeSequences",
type: "POST",
data: {
Type: type
},
success: function(reponse) {
var alertes = JSON.parse(reponse);
for (var i = 0; i < alertes.length; i++) {
var alerte = new Object();
alerte.DynamicDataId = "alerte" + alertes[i]["DynamicAlerteId"];
alerte.Message = alertes[i]["Message"];
alerte.AddressMac = alertes[i]["AddressMac"];
alerte.ZoneName = alertes[i]["ZoneName"];
alerte.DepartmentName = alertes[i]["DepartmentName"];
alerte.GroupName = alertes[i]["GroupName"];
alerte.UsersDisplayName = alertes[i]["UsersDisplayName"];
alerte.LastUpdate = alertes[i]["LastUpdate"];
alerte.Profile = alertes[i]["Profile"];
alerte.TypeId = alertes[i]["TypeId"];
alerte.ZoneId = alertes[i]["ZoneId"];
alerte.Date = alertes[i]["Date"];
alerte.TypeAlarme = alertes[i]["TypeAlarme"];
alerte.Status = GetStatusData(DynamicAlarmesId, alertes[i]["DynamicAlerteId"])
if (!existInDynamicData(alerte.DynamicDataId)) {
Alertes.push(alerte);
}
}
}
});
}
setInterval(GetData(), 3000);
function GetData() {
GetAlertes();
FindNewAlertes();
}
function FindNewAlertes() {
const alertesCodeBlanc = Alertes.filter(alerte => alerte.TypeAlarme === 'Emergency');
const actualTime = new Date();
for (var i = 0; i < alertesCodeBlanc.length; i++) {
const codeBlancTime = new Date(alertesCodeBlanc[i].LastUpdate);
const timeElapsed = (actualTime.getTime() - codeBlancTime.getTime());
//console.log('temptEcoule: ' + tempsEcoule);
if (timeElapsed <= 5000) {
console.log('render modal called');
RenderModal_Alerte(alertesCodeBlanc[i].Message);
}
}
}
但是,问题是RenderModal_Alerte()被调用的次数与console.log()相同。我没看到错误在哪里。如果有人能告诉我出什么问题了,我将不胜感激。