Created
February 25, 2022 13:01
-
-
Save bartwttewaall/2a2893c19588378db9651bc2bf1f56ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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