I want to do a cool CSS background change to match with the video.
I use this code:
document.getElementById("video").addEventListener("playing",video,false);
function video(){
if(document.getElementById('video').currentTime >= 3.466) {
document.getElementById("body").style.backgroundColor = "black";
} else {
document.getElementById('video').addEventListener("timeupdate", video());
}
}
I was hoping that when the video reaches 3.466 seconds it would change the color of the background but, it doesn’t and Firefox says that there is too much recursion.
2
Answers
so
timeupdate
is the event you’re looking for. You can listen to it once and check to see if thecurrentTime
is past your 3.466 seconds. I put a CodePen together – https://codepen.io/motionharvest/pen/yLGqwqZ?editors=1111Best of luck.
Event listener checks the current time of the video and changes the background