I'm building a website witch contains a 15 youtube embeds on 1 page, and I have a youtube-api javascript code witch plays them continuously, and changes a class that surrounds the embed. The problem is that the code occasionally doesn't fire, and this only happens online, not on my computer. I think this is because it sometimes takes too mutch time to load the 15 embeds with a slow connection, maybe the code fires when the players aren't ready, or too late? well... I dont know. I tried wrapping the whole code in a SetTimout() thing, but no luck. Anybody have a solution? Help will be greatly appreciated!
CODE (PARTIALLY):
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player1;var player2;
function onYouTubeIframeAPIReady() {
player1 = new YT.Player('player1', {
events: {
'onStateChange': onPlayerStateChange1
}
});
player2 = new YT.Player('player2', {
events: {
'onStateChange': onPlayerStateChange2
}
});
---- etc... (untill player 15)
function onPlayerStateChange1(event) {
if (event.data == 0) {
player2.playVideo();
};
if (event.data == 1) {
document.getElementById("redlow1").setAttribute("class", "hero-unit77");
document.getElementById("redhigh1").setAttribute("class", "hero-unit88");
};
if (event.data == 2 || event.data == 0 || event.data == 5) {
document.getElementById("redlow1").setAttribute("class", "hero-unit7");
document.getElementById("redhigh1").setAttribute("class", "hero-unit8");
};
}
function onPlayerStateChange2(event) {
if (event.data == 0) {
player3.playVideo();
};
if (event.data == 1) {
document.getElementById("redlow2").setAttribute("class", "hero-unit77");
document.getElementById("redhigh2").setAttribute("class", "hero-unit88");
};
if (event.data == 2 || event.data == 0 || event.data == 5) {
document.getElementById("redlow2").setAttribute("class", "hero-unit7");
document.getElementById("redhigh2").setAttribute("class", "hero-unit8");
};
}
---- etc... (untill player 15)