This has me totally confused. I'm trying to run through a series of Youtube ids to check if the videos still exist.
I have all the ids in an array and want to check the gdata api to see if I get XML results or a Video not found 404 message.
I found a function on SO to check each id and add a row to a table.
var ytarray = ARRAY OF YOUTUBE IDs
var yttitles = ARRAY OF MATCHING TITLES FOR THE VIDS
var urlExists = function(url, callback){
$.ajax({
type: 'HEAD',
url: url,
success: function() {
callback(true);
},
error: function() {
callback(false);
}
});
}
var length = ytarray.length,
for (var i = 0; i < length; i++) {
urlExists('http://gdata.youtube.com/feeds/api/videos/'ytarray[i], function(success) {
if (success) {
$('#rows').append('<tr><td>'+yttitles[i]+'</td><td>'+ytarray[i]+'</td><td style="color: green">Found</td></tr>');
} else {
$('#rows').append('<tr><td>'+yttitles[i]+'</td><td>'+ytarray[i]+'</td><td style="color: red">Not Found</td></tr>');
}
});
};
});
});
The table fills out but all of the rows contain the information for the last video not each one as it goes through the loop.
Testing with alerts it seems the table doesn't get filled out until all the for loops have completed.
I wonder if this is something to do with me not handling the asynchronous nature of AJAX correctly?