I have an ajax function which loads thumbnails which is called by another ajax function and gets the parameter from there in the loop.
I have tried waiting for the ajax call to be finished by using .done and .ajaxComplete but the function still seems to fire while the ajax is still calling.
But it seems that the array is undefined until the last call is made by the Ajax.
Here is a codePen - https://codepen.io/anon/pen/oermYj
//POPULATE IFRAMES FUNCTION
var counter = 0,
mainTitle,
iframeLink,
slideIndex,
iframeID,
iframeCount,
iframeTotal,
iframeTitles,
iframeThumbnail;
var iframeTitleArray = [];
function getIframeInfo(uniqueID){
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql',
data: {
q: "select * from json where url ='https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=" + uniqueID + "&format=json'",
format: "json"
},
dataType: "jsonp",
success: function (data) {
iframeTitles = data.query.results.json.title;
},
error: function (result) {
alert("Sorry no data found.");
}
}).done(function pushTitles(){
iframeTitleArray.push(iframeTitles);
});
}
console.log(iframeTitleArray);
function populateCars(carId){
$.ajax({
type : 'GET',
url : 'json/data2.json',
data : {
get_param: 'value'
},
dataType : 'json',
success : function(data){
//DEFAULT DATA PLACEMENT
$('.gallery-slider').empty();
$('.gallery-sub-slider').empty();
$('.grid-center-wrap').empty();
//LOOP THROUGH JSON AND POPULATE
$.each(data.iframeImages, function(i){
iframeThumbnail = data.iframeImages[i].iframeThumb;
iframeLink = data.iframeImages[i].iframeLink;
iframeID = data.iframeImages[i].iframeID;
iframeCount = i + 1;
$('.gallery-slider').append('<iframe width="590" height="950" src="' + iframeLink + '" frameborder="0" allowfullscreen></iframe>');
$('.grid-center-wrap').append('<div class="grid-block"><img class="grid-img" src="//img.youtube.com/vi/' + iframeThumbnail + '/0.jpg"><div class="grid-overlay" data-slick-index="' + i + '"></div><div class="grid-number">' + (iframeCount + ' / ' + data.iframeImages.length) + '</div></div>');
$('.gallery-mobile-slider').append('<div><img src="//img.youtube.com/vi/' + iframeThumbnail + '/0.jpg" class="mobile-gallery-img" data-slick-index="' + i + '"></div>');
getIframeInfo(iframeThumbnail);
});
//DEFAULT IMAGE COUNTER VALUE
$('.slider-count').html('1' + ' / ' + iframeCount);
//WORKING OUT SUB-GALLERY DIV AMOUNT, POPULATE
var divCount = Math.ceil(data.iframeImages.length);
for (var i = 0; i < divCount; i=i+2) {
if(data.iframeImages[i+1] == undefined){
console.log('not here');
$('.gallery-sub-slider').append(`<div class="sub-gallery-item" data-slick-index="${i}"><img src="//img.youtube.com/vi/${data.iframeImages[i].iframeThumb}/0.jpg" class="sub-gallery-img" data-slick-index="${i}"></div>`);
return;
}
else{
$('.gallery-sub-slider').append(`<div class="sub-gallery-item" data-slick-index="${i}">
<img src="//img.youtube.com/vi/${data.iframeImages[i].iframeThumb}/0.jpg" class="sub-gallery-img" data-slick-index="${i}">
<img src="//img.youtube.com/vi/${data.iframeImages[i + 1].iframeThumb}/0.jpg" class="sub-gallery-img" data-slick-index="${i + 1}">
</div>`);
}
}
}
}).done(function(){
//INITIALIZE SLIDERS
createSlider();
});
}
populateCars();