JavaScript Run a function a few seconds before video ends using JavaScript

I have a video the being played. How can I call a function 5 seconds before the end of the video?

I thought to put a timer when I start the video, the problem is that the user can control the video, and stop it and play again how many times he wants, so the timer is getting ineffective.

Examples would be very nice!

-----EDIT----- this is an example to a code i'm using:

video = document.createElement("video");
    video.setAttribute("id", id);
    video.src = url;
    video.onended = function(e) {
        var playlistName = getPlaylistName();
        findNextSongToPlay(playlistName, playNextSong);
    };

I want the "onended" event to be called not in the end, but 5 seconds before.. So I need to change it a little bit..

Again, Thanks a lot!

Answer:1

Here is a demo for you

Edit: Updated Demo.

window.onload=function(){
video = document.createElement("video");
    video.setAttribute("id", "Myvideo");
    video.setAttribute("controls", "controls");
    video.src = "http://www.w3schools.com/html/mov_bbb.mp4"; 
    video.addEventListener("timeupdate", myfunc,false);
    document.body.appendChild(video);  
}

function myfunc(){
	if(this.currentTime > this.duration-5){
		//Less than 5 seconds to go. Do something here.

//---- For Demo display purposes
document.getElementById('Example').innerHTML="Less than 5 seconds to go!";
//---------
	} //End Of If condition.
//---- For Demo display purposes
else{document.getElementById('Example').innerHTML="";}
//---------
}
<div id="Example"></div>
Answer:2

I want to check if a server is reachable with a JavaScript function. By reachable, I mean, if the server answers, I don't care which HTTP status code, it's reachable. Stackoverflow/Google helped me ...

I want to check if a server is reachable with a JavaScript function. By reachable, I mean, if the server answers, I don't care which HTTP status code, it's reachable. Stackoverflow/Google helped me ...

  1. online check deposit resource center

I've read that you should avoid nested promises in JavaScript, as they tend to be an antipattern, but I'm having trouble figuring out how to avoid them in my particular use case. Hopefully someone ...

I've read that you should avoid nested promises in JavaScript, as they tend to be an antipattern, but I'm having trouble figuring out how to avoid them in my particular use case. Hopefully someone ...

I'm building a web app that has to work on very old browsers (IE 8, Firefox 20, etc). Can I rely on document.readyState being interactive and complete working correctly? Basically, I want to perform ...

I'm building a web app that has to work on very old browsers (IE 8, Firefox 20, etc). Can I rely on document.readyState being interactive and complete working correctly? Basically, I want to perform ...

I'm trying to look for certain elements if they contain a certain class in a Wordpress Widget. But what I can't understand is why the authors.hasClass() is OK, but the currentAuthor.hasClass() throws ...

I'm trying to look for certain elements if they contain a certain class in a Wordpress Widget. But what I can't understand is why the authors.hasClass() is OK, but the currentAuthor.hasClass() throws ...

  1. jquery hasclass doesn't work
  2. jquery doesn't hasclass
  3. jquery does not has class
  4. jquery hasclass does not work