Reputation: 83
The basic idea is to identify if the page has a video, wait until it's over and then keep sliding the pages that don't contain the video with interval of 30 sec. It clears interval and waits until it finishes only once. In the second time it just keeps sliding like other pages.
function getPage(i) {
return p[i];
}
var pages = '@ViewBag.pages';
var p = JSON.parse(pages.replace(/("\;)/g, "\""));
var i = 0;
var mi = p.length - 1;
var interval;
var frame = document.getElementById("slider");
var frameDoc = frame.contentDocument || frame.contentWindow.document;
frame.onload = function() {
if ($(document).find("iframe").contents().find("video").length == 1) {
clearInterval(interval);
console.log("interval was cleared successfully");
console.log("the video is now playing!");
var video = document.getElementById('slider').contentWindow.document.getElementById('video');
video.onended = function() {
console.log("video is finished!");
i++;
if (i > mi) {
i = 0
}
frame.src = getPage(i);
}
} else {
console.log("video is not found");
interval = setInterval(function() {
i++;
if (i > mi) {
i = 0
}
$.ajax({
async: false,
url: getPage(i),
type: 'GET',
success: function() {
frame.src = getPage(i);
}
});
}, 10000);
}
}
Upvotes: 0
Views: 56
Reputation: 2323
The problem here is that clearInterval
is not getting called when the page doesn't have a video.
Let's take the following scenario, you have 2 pages without video and 1 page with video in that order
interval
.interval
is updated with a new setInterval. Now you have two setIntervals running, lets say interval1
and interval2
interval1
moves the page to 3rd page and interval2
moves the page to 1st page.var interval;
frame.onload = function() {
interval && clearInterval(interval);
if ($(document).find("iframe").contents().find("video").length == 1) {
} else {
interval = setInterval(function() {}, 10000);
}
}
frame.onload = function() {
if ($(document).find("iframe").contents().find("video").length == 1) {
} else {
setTimeout(function() {}, 10000);
}
}
PS: I prefer the second one
Upvotes: 1