var youtubePlayers = []; function onYouTubeIframeAPIReady() { var tag = document.createElement('script'); tag.src = "https://www.youtube.com/iframe_api"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); carregouYoutubes = 1; document.removeEventListener('touchstart', onYouTubeIframeAPIReady); document.removeEventListener('mousedown', onYouTubeIframeAPIReady); document.removeEventListener('scroll', onYouTubeIframeAPIReady); var youtubeVideos = document.querySelectorAll('.youtube-video:not([data-loaded="true"])'); for (var i = 0; i < youtubeVideos.length; i++) { var youtubeVideo = youtubeVideos[i]; var youtubePlayer = new YT.Player(youtubeVideo, { height: '360', width: '640', videoId: youtubeVideo.dataset.id }); youtubePlayers.push(youtubePlayer); youtubeVideo.setAttribute('data-loaded', 'true'); } } var carregouYoutubes = 0; setTimeout(function() { if (carregouYoutubes==0) { onYouTubeIframeAPIReady(); } }, 6000); document.addEventListener('touchstart', onYouTubeIframeAPIReady); document.addEventListener('mousemove', onYouTubeIframeAPIReady); document.addEventListener('scroll', onYouTubeIframeAPIReady);