Use postMessage for communicating waitUntil

This commit is contained in:
Henri Bergius 2018-12-26 16:51:28 +01:00
parent 173311ed33
commit 8fd8d7a3bd
3 changed files with 26 additions and 2 deletions

View file

@ -61,13 +61,17 @@
var video = document.createElement('video'); var video = document.createElement('video');
video.setAttribute('src', prefix + file + '.mp4'); video.setAttribute('src', prefix + file + '.mp4');
video.autoplay = true; video.autoplay = true;
video.loop = true;
video.addEventListener('playing', function () { video.addEventListener('playing', function () {
if (window.waitUntil) { if (window.waitUntil) {
return; return;
} }
var now = new Date(); var now = new Date();
window.waitUntil = now.getTime() + ((video.duration - video.currentTime) * 1000); window.waitUntil = now.getTime() + ((video.duration - video.currentTime) * 1000);
if (window.parent) {
window.parent.postMessage({
waitUntil: window.waitUntil,
}, '*');
}
}); });
document.body.appendChild(video); document.body.appendChild(video);

View file

@ -62,6 +62,7 @@ function DisplayParticipant(broker, role, defaultUrls, timer) {
callback('opened', null, next.getAttribute('src')); callback('opened', null, next.getAttribute('src'));
return; return;
} }
window.onmessage = null;
next.onerror = (err) => { next.onerror = (err) => {
next.onload = null; next.onload = null;
next.onerror = null; next.onerror = null;
@ -80,6 +81,21 @@ function DisplayParticipant(broker, role, defaultUrls, timer) {
timeout = setTimeout(() => { timeout = setTimeout(() => {
participant.send('open', getRotationUrl(urls, indata)); participant.send('open', getRotationUrl(urls, indata));
}, timer); }, timer);
window.onmessage = (event) => {
window.onmessage = null;
const now = new Date();
if (!event.data
|| !event.data.waitUntil
|| event.data.waitUntil < now.getTime()) {
return;
}
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
participant.send('open', getRotationUrl(urls, indata));
}, event.data.waitUntil - now.getTime());
};
callback('opened', null, next.getAttribute('src')); callback('opened', null, next.getAttribute('src'));
}; };
next.setAttribute('src', indata); next.setAttribute('src', indata);

View file

@ -61,13 +61,17 @@
var video = document.createElement('video'); var video = document.createElement('video');
video.setAttribute('src', prefix + file + '.mp4'); video.setAttribute('src', prefix + file + '.mp4');
video.autoplay = true; video.autoplay = true;
video.loop = true;
video.addEventListener('playing', function () { video.addEventListener('playing', function () {
if (window.waitUntil) { if (window.waitUntil) {
return; return;
} }
var now = new Date(); var now = new Date();
window.waitUntil = now.getTime() + ((video.duration - video.currentTime) * 1000); window.waitUntil = now.getTime() + ((video.duration - video.currentTime) * 1000);
if (window.parent) {
window.parent.postMessage({
waitUntil: window.waitUntil,
}, '*');
}
}); });
document.body.appendChild(video); document.body.appendChild(video);