I have a piece of a function where I need to iterate the loop on 15sec interval. I have already tried with setInterval()
with timer value 15000 miliseconds(15 sec) but that is not working at all. I cannot use setTimeout()
as I have to repeat the whole iteration of code inside the block. The question may look like a duplicate of other questions in Stackoverflow but the references and the examples posted is not working for my scenario. Please advice for the code given below.
EDIT :: Rickard (clarification by OP from comment)
On each iteration, I first need to check a value from the database. If the db returned value matches with the condition the iteration will execute its value and delay for 15secs for next iteration followed by the same db check. If on any iteration the db check does not match then the loop will exit at once. That is what I tried to do in the piece of code
acTripSess =[{el:Jhon, isIdleActive:1},{el:Chris, isIdleActive:1}]
for (i = 0; i < acTripSess.length; i++) {
acTripItem = acTripSess[i];
//console.log(acTripItem);
(function (i) {
setInterval(function () {
if (acTripItem.isIdleActive == 1) {
title = 'fleet_' + acTripItem.el; msg = 'tripFor_'+i;
console.log(msg + ' notification on ' + title);
}
}, 15000);
})(i)
}
EDIT
I even tried the same on another way but the loop is skipping on the 2nd element.
acTripSess = [
{el: 'Jhon', isIdleActive: 1},
{el: 'Chris', isIdleActive: 1},
{el: 'Steve', isIdleActive: 1}
];
for (var key in acTripSess) {
var acTripItem = acTripSess[key];
//console.log(acTripItem);
if (key == 0) {
topic = 'driver_' + acTripItem.el;
msg = 'newBooking_' + key;
console.log(msg+' published on '+topic);
}
else {
setTimeout(function(){
topic = 'driver_' + acTripItem.el;
msg = 'newBooking_' + key;
console.log(msg + ' published on ' + topic);
},1000)
continue;
}
}