I am using videojs
for displaying a film, and depending on the time of the video (timeupdate), I display a description from the backend, that is required for this project.
In case of a problem with loading the description on time (but not with buffering of the video), I’d like to stop the video, for example after 3 seconds delay, and continue the video when the description fetch finishes downloading the data.
I am not sure whether I can use a wrapped setTimeout
and fetch with Promise for solving this problem.
Is it better to repeat fetch after video has stopped by using AbortController
? Is this a proper way to approach this?
This is what I use for downloading the data without setTimeout
and starting/stopping the video:
const player = videojs('my-video');
player.on('timeupdate', async function(){
//...
const verset = await checkVerset(zone);
//...
});
async function checkVerset(zone) {
let zoneRes;
await connectFetch('./in.php', zone ).then(resolve =>{
zoneRes = resolve;
console.log(zoneRes);
}).catch(error => console.log( 'Error of details: ', error));
return zoneRes;
}
async function connectFetch (url, text) {
const response = await fetch(url, {
method:'post',
mode:'cors',
credentials:'same-origin',
headers:{'Content-type':'text/plain;charset=UTF-8'},
body: JSON.stringify(text)
})
if(!response.ok){
const message = 'Error: ${response.status}';
throw new Error(message);
}
const resolve = response.text();
return resolve;
}
//player.pause();
//player.play();
2
Answers
I have edited function by add setTimeout and clearTimeout. Now program is working properly even with small time(500ms). For smooth play video I use more period and I will try implement function of download data ahead of proper time displaying descriptions.
Thank you
Here is a basic implementation (that can further be optimized) that you can use a start point:
It’s important to point, that the
threshold
must be less than the actual timestamp distance, as we might run into race conditions which means the code will require substantial additional checks. I am keeping it simple for now, and setting threshold to 2".Sidenote: I came up with this quick
Excalidraw
diagram before diving into the code, just to let my intuition guide the implementation.