Question

Currently trying to detect when a YouTube video in a playlist ends so I can play the next one ( Adding event handler to loop through dynamically created playlist (YouTube API) ). I'm now trying to figure out when one variable is equal to another, so I can navigate to the next video in the playlist.

Here is the code to detect when a video has ended and save the video ID to a div (stopID):

function stopCycle(event) {
      if (event.data == YT.PlayerState.ENDED) {
           var url = event.target.getVideoUrl();
           var match = url.match(/[?&]v=([^&]+)/);
           var videoId = match[1];
           $('#stopID').html(videoId);
      }
  }

And here is my attempt at detecting (and testing with an alert) when the video ID of the video clicked equals the video ID of the in stopCycle (i.e. when the video ends):

    $('#Playlist').on('click', '.playlistYT', function(){
       $('#subHistory').click();
       videoID = $(this).attr("src").slice(27,38);
       $('#infoID').val(videoID);
       player.loadVideoById(videoID ); 
       var nextVideo = $(this).parent().next().find('.playlistYT');

      $.when($('#stopID').html() == videoID).then(function(){
          alert("asd");
      });

Currently, I get the alert immediately, i.e. before the stopID div even has a video ID in it. Where am I going wrong with $.when function? Should I be sticking to an if/else statement?

Était-ce utile?

La solution

$.when takes in a promise argument. Just use an if conditional to check equality.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top