Sometimes, we want to prevent "The play() request was interrupted by a call to pause()" error with JavaScript.
In this article, we’ll look at how to prevent "The play() request was interrupted by a call to pause()" error with JavaScript.
How to prevent "The play() request was interrupted by a call to pause()" error with JavaScript?
To prevent "The play() request was interrupted by a call to pause()" error with JavaScript, we can check if the play is called only when the video is paused.
For instance, we write
let isPlaying = true;
video.onplaying = () => {
isPlaying = true;
};
video.onpause = () => {
isPlaying = false;
};
const playVid = () => {
if (video.paused && !isPlaying) {
return video.play();
}
};
const pauseVid = () => {
if (!video.paused && isPlaying) {
video.pause();
}
};
to set isPlaying to true when the video is playing.
And we set it to false when we pause the video.
We call video.play when the video is paused and isPlaying is false.
And we call video.pause when the video isn’t paused and isPlaying is true.
Conclusion
To prevent "The play() request was interrupted by a call to pause()" error with JavaScript, we can check if the play is called only when the video is paused.