Skip to content

Instantly share code, notes, and snippets.

@bartwttewaall
Created February 25, 2022 13:01
Show Gist options
  • Save bartwttewaall/2a2893c19588378db9651bc2bf1f56ed to your computer and use it in GitHub Desktop.
Save bartwttewaall/2a2893c19588378db9651bc2bf1f56ed to your computer and use it in GitHub Desktop.
/**
* @param {HTMLVideoElement} videoElement
*/
export function waitForVideoPlaying(videoElement, duration = 500) {
return new Promise((resolve, _reject) => {
const seconds = duration / 1000;
const timeUpdateHandler = (evt) => {
if (evt.target.currentTime > seconds) {
evt.target.removeEventListener('timeupdate', timeUpdateHandler);
resolve();
}
};
if (videoElement.currentTime > seconds) resolve();
else videoElement.addEventListener('timeupdate', timeUpdateHandler);
});
}
/**
* @param {HTMLVideoElement} videoElement
*/
export function waitForVideoPlayThrough(videoElement) {
return new Promise((resolve, _reject) => {
const canplaythroughHandler = (evt) => {
if (evt.target.readyState >= 4) {
evt.target.removeEventListener('canplaythrough', canplaythroughHandler);
resolve();
}
};
if (videoElement.readyState >= 4) resolve();
else videoElement.addEventListener('canplaythrough', canplaythroughHandler);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment