I want to exit out of the jquery $.each loop when when the exit variable in my function is 1.
Below is the API response :
{
"data": [{
"name": "rotate",
"game": [{
"id": "1"
},
{
"id": "2"
}]
},
{
"name": "skew",
"game": [{
"id": "32"
}]
}
]
}
New entries added:
{
"data": [{
"name": "rotate",
"game": [{
"id": "1"
},
{
"id": "2"
}]
},
{
"name": "skew",
"game": [{
"id": "32"
}]
},
{
"name": "move",
"game": [{
"id": "12"
},
{
"id": "14"
}]
},
{
"name": "update",
"game": [{
"id": "8"
}]
}
]
}
And the code I am using is :
function getdata(response) {
var exit = 0;
$.each(response.data, function( key, value ) {
if(value.game.length > 1) {
firebase.database().ref(`data/${value.name}`).once("value", snapshot => {
if (snapshot.exists()) {
exit = 0;
} else {
changeVideo(data);
exit = 1;
}
}).catch((error) => {
console.error(error);
});
}
else {
firebase.database().ref(`data/${value.name}`).once("value", snapshot => {
if (snapshot.exists()) {
exit = 0;
} else {
changeVideo(data);
exit = 1;
}
}).catch((error) => {
console.error(error);
});
}
});
}
Which calls a change video function :
function changeVideo(data) {
console.log("Change video called");
var video = document.getElementById('video');
var name = data.name;
video.setAttribute("src", "videos/"+name+".mp4");
video.load();
if (video.paused) {
video.play();
}
$("#video").show().delay(7000).queue(function (next) {
$(this).hide();
showtable(data);
$(".blackcontainer").show().delay(10000).queue(function (next) {
writetofirebase(data);
next();
});
next();
});
}
Basically, the code is to display a video if and only if there is any new item in my API response. If new item is there, I want to display the video and then hide that video and show a table. then I write that value to firebase so that next time I call the api and compare with firebase data, I dont want to show the video if it is already shown. But when n new entries comes in the API response, Everything breaks. and the function changevideo is called n times. I want to exit when the first video is played.
I am also using a
setInterval(function() {getdata();}, 60000);
for calling the function each 60 seconds. I want only one video to play in the 60 seconds.
then only in the next call, it needs to play the next video even if there are multiple new entries in the API response.