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


i have video being played. how can call function 5 seconds before end of video?

i thought put timer when start video, problem user can control video, , stop , play again how many times wants, timer getting ineffective.

examples nice!

-----edit----- example 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 "onended" event called not in end, 5 seconds before.. need change little bit..

again, lot!

here demo

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 5 seconds go. here.    //---- demo display purposes  document.getelementbyid('example').innerhtml="less 5 seconds go!";  //---------  	} //end of if condition.  //---- demo display purposes  else{document.getelementbyid('example').innerhtml="";}  //---------  }
<div id="example"></div>

since player dynamically created can use:

video = document.createelement("video"); video.setattribute("id", id); video.src = url;   //add event listener   video.addeventlistener("timeupdate", myfunc,false);   video.onended = function(e) {     var playlistname = getplaylistname();     findnextsongtoplay(playlistname, playnextsong);   }; 

and should call function

function myfunc(){     if(this.currenttime > this.duration-5){         //do here..     } } 

if have questions please leave comment below , possible.

i hope helps. happy coding!


Comments