I'm trying to clear an interval calling the function to check the condition inside an ajax but every 5 seconds I'm still getting the message so the interval is not cleared. What am I doing wrong? I call the function inside the ajax 'update tarea' and I send the interval name and the checked condition but the interval is still running
function update_task(update_tarea, interval){
if(update_tarea === "si"){
window.clearInterval(interval);
swal({
title: "Se ha fijado el próximo recordatorio",
icon: "success"
});
}
}
//get info profile
$.ajax({
url: 'recordatorio.php',
type: 'post',
data: { iduser: iduser},
dataType: 'json',
success: function (response) {
if(response.validacion === "ok"){
var tareas = response['mascotas'];
var fecha = new Date();
var mes = fecha.getMonth() + 1;
var dia = fecha.getDate();
var año = fecha.getFullYear();
var fechaHoy = año + '-' + mes + '-' + dia;
var len = tareas.length;
for(i=0; i<len; i++){
var fechaTarea= tareas[i]['fecha'];
var idpetTarea = tareas[i]['idpet'];
var idtipo = tareas[i]['idtipo'];
var notas = tareas[i]['notas'];
var horaTarea = tareas[i]['hora'];
var tipotarea = tareas[i]['tipo'];
var nombremascota = tareas[i]['petname'];
if(fechaHoy === fechaTarea){
var interval= idtipo+idpetTarea+"interval";
window.interval = setInterval(function () {
var hH = new Date();
var horas = hH.getHours();
var minutos = hH.getMinutes();
var segundos = hH.getSeconds();
var horaHoy = horas + ':' + minutos + ':' + segundos;
var string= "Recuerda la tarea <b>''"+tipotarea+"''</b> para tu mascota <b>"+nombremascota+"</b></br>Notas:"+notas;
if(horaHoy === horaTarea || horaHoy > horaTarea){
swal({
title: "Recordatorio",
text: "Recuerda la tarea <b>''"+tipotarea+"''</b> para tu mascota <b>"+nombremascota+"</b></br><b>Notas</b>:"+notas,
html: true,
icon: "info"
});
//update tarea
window.update_tarea;
$.ajax({
url: 'updatetarea.php',
type: 'post',
data: { idpetTarea: idpetTarea, idtipo: idtipo },
dataType: 'json',
async : false,
success: function (response) {
if(response === "ok"){
update_tarea= "si";
update_task(update_tarea, interval);
}
}
});
}
}, 5000); //end interval
}
}
}
}
});