The documentation at your given link shows one way to use the YouTube API.
What it's doing, is running some code as soon as the HTML is loaded in the browser.
The code below is code that I've copied from the example, showing the client side code that is running automatically when the page loads because it's in the global scope.
I think what you need to do, is to remove SOME of that code from the global scope, and put it into a function, and then call that new function from your success handler function.
Your success handler function is "vidurls"
So, the vidurls function should call some other function that has the below code in it.
CURRENTLY:
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player; TRY:
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "
https://www.youtube.com/iframe_api";
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function vidurls(data) {
for( i = 0; i < data.length; i++){
videos.push = data[i][3];
console.log("data = " + data[i][3] );
console.log("videos pushed = " + videos);
}
createIframes();//Create the Iframes
}
window.createIframes = function() {
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
I probably got something wrong. I have never used this code, and have no experience with this.
But, what you want to do is to prevent some of that code from running until after your success handler has updated the array.
My attempt might be totally wrong, I don't know.
You could try it and see what happens.
Maybe it will give you an idea.