youtube-javascript-api

Triggering an event when YouTube video reaches certain timestamp?


The API documentation does not contain an obvious way to trigger a javascript event when a YouTube video reaches a certain time stamp. Do you know any workaround?


Solution

  • You can create a timer which check at second whether video has reached certain timestamp. To restrict unnecessary function calls, start time only when it reaches your set time

    var timeout = setTimeout(function(){
      var interval = setInterval(function(){
        if(player.getCurrentTime() === <you_set_time>){
            clearInterval(interval);
            // Your logic
        }
    
      },1000);
    },<your_set_time_in_millisecs>);