jQuery 2.0 | Youtube API 2 - Get data of each video ID from Playlist feed excluding private or deleted videos

jQuery 2.0 | Youtube API 2 - Get data of each video ID from Playlist feed excluding private or deleted videos

Using the Youtube API 2 for embedded iframe, and after dynamically building a list of videos from a playlist feed which is rid of private or deleted videos, I aim to retrieve the duration of each fetched video through jsonc, kinda like so:

  1. // Code to first display playlist feed and remove private or deleted videos (if any) not included here.
  2.  . . .
  3. var vid = Array();
  4. $(".list:visible").find('.videoID').each(function(data){
  5. vid[data] = $(this).text();
  6. var video = '//gdata.youtube.com/feeds/api/videos/' + vid + '?v=2&alt=jsonc';
  7. $.getJSON(video, function(data,status,xhr) {
  8. console.log(data.data.duration);
  9. // Here would be code to append transformed duration (hours:mins:secs) of each video (thumbnail) into their span containers.
  10. });
  11. });

Is the above legit? I guess I have to do it this way since beforehand in some feeds there are private and deleted videos that must be deleted from the list, so I fetch all videos in the playlist feed, then immediately remove all videos components whose title equals Private video or Deleted Video, then iterate the code above throughout remaining video IDs. I do it this way because the code breaks if it encounters a private or deleted video element thats exists within the list (no data.data.duration to return?).

Problem is, the above code doesn't loop through all dynamically set .videoID. Console.log only prints the duration of first .videoID displayed. I also tried setTimeout in order to wait a few to make sure the playlist feed had completely displayed and that didn't help either.

Is there a better and more efficient way to do the above? Am kinda newbie to all this ajax jsonc thingy, any pointers leading to an efficient solution would be mighty helpful! ~ dan