Iframe API and event onStateChange♪
Event onStateChange He'll return 0 when the reproduction is over.
Truth is, if you need a user to look at all the videos, there's no rewear, that's, like, 0 coming back when the video reaches the end, it doesn't matter if it's overwhelmed by the user or not.The details of the Iframe API and the events can be read here:API - https://developers.google.com/youtube/js_api_reference?hl=ru Developments - https://developers.google.com/youtube/js_api_reference?hl=ru#EventHandlers // Загружаем Iframe API
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
//Добавляем видео в элемент с ID player
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onReady //Функция, которая вызовется когда видео загружено
}
});
}
function onReady() {
//Событие onStateChange вернет 0 когда воспроизведение окончено
player.addEventListener('onStateChange', function(e) {
console.log('State is:', e.data);
if(e.data == 0){
//Выполняем нужные действия
}
});
}<div id="player"></div>