Skip to content

Commit

Permalink
clear existing timeout on new Set_Target or pause
Browse files Browse the repository at this point in the history
  • Loading branch information
edsilv committed Sep 25, 2024
1 parent 24cb668 commit 53084e9
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class MediaElementCenterPanel extends CenterPanel<
mediaWidth: number;
player: any;
title: string | null;
pauseTimeoutId: any = null;

constructor($element: JQuery) {
super($element);
Expand All @@ -59,6 +60,12 @@ export class MediaElementCenterPanel extends CenterPanel<
});

this.extensionHost.subscribe(IIIFEvents.SET_TARGET, (target: TFragment) => {
// Clear any existing timeout
if (this.pauseTimeoutId !== null) {
clearTimeout(this.pauseTimeoutId);
this.pauseTimeoutId = null;
}

let t: number | [number, number] = target.t;
if (Array.isArray(t)) {
if ((t as [number] | [number, number]).length === 1) {
Expand All @@ -78,8 +85,9 @@ export class MediaElementCenterPanel extends CenterPanel<

const duration = (endTime - startTime) * 1000;

setTimeout(() => {
this.pauseTimeoutId = setTimeout(() => {
this.player.pause();
this.pauseTimeoutId = null; // Clear the timeout ID after execution
}, duration);
}

Expand Down Expand Up @@ -217,6 +225,10 @@ export class MediaElementCenterPanel extends CenterPanel<
});

mediaElement.addEventListener("pause", () => {
if (this.pauseTimeoutId !== null) {
clearTimeout(this.pauseTimeoutId);
this.pauseTimeoutId = null;
}
// mediaelement creates a pause event before the ended event. ignore this.
if (
Math.floor(mediaElement.currentTime) !=
Expand Down Expand Up @@ -281,6 +293,10 @@ export class MediaElementCenterPanel extends CenterPanel<
});

mediaElement.addEventListener("pause", () => {
if (this.pauseTimeoutId !== null) {
clearTimeout(this.pauseTimeoutId);
this.pauseTimeoutId = null;
}
// mediaelement creates a pause event before the ended event. ignore this.
if (
Math.floor(mediaElement.currentTime) !=
Expand Down

0 comments on commit 53084e9

Please sign in to comment.