JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://raw.github.com/padolsey/jQuery-Plugins/master/sortElements/jquery.sortElements.js"></script>
<div>
Videos loaded: <span id="VideosLoaded">0</span>
</div>
<div>
<select id="MaxVideos">
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="500">500</option>
<option value="10000">all videos</option>
</select>
<input id="LoadVideos" type="button" value="Load" style="width:50px;" />
<div id="LoadingDiv">
<img src="http://www.google.com/mapmaker/mapfiles/spinner.gif" />
<em>Loading videos...</em>
</div>
</div>
<table>
<thead>
<tr>
<th id="Title">Title</th>
<th id="Duration">Duration (sec)</th>
</tr>
</thead>
<tbody id="videos">
</tbody>
</table>
CSS
#LoadingDiv {
color:#ccc;
}
div, table {
margin: 1em;
}
th {
white-space: nowrap;
font-weight:bold;
cursor: pointer;
}
td,th {
border: 1px solid #aaa;
padding:.25em;
}
JavaScript
// Only show spinner when AJAX is running
$('#LoadingDiv')
.hide() // hide it initially
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
})
;
// LoadVideos button
$('#LoadVideos').click(function(){
getVideos(1, $('#MaxVideos').val());
});
// Recursive function to grab the next set of videos
function getVideos(index, max) {
$.ajax({
url : 'https://gdata.youtube.com/feeds/api/playlists/UUAuUUnT6oDeKwE6v1NGQxug?v=2&orderby=duration&max-results=50&start-index=' + index,
// 'https://gdata.youtube.com/feeds/api/users/tedtalksdirector/uploads',
dataType : "xml",
success : function(xml) {
var videos = $(xml).find("entry");
// Sort on each AJAX call
//videos.sort(function(a, b){
// return (parseInt($(b).find("duration").attr("seconds")) - parseInt($(a).find("duration").attr("seconds")));
// });
videos.each(function(){
var title = $(this).find("title").text();
var duration = $(this).find("duration").attr("seconds");
var minutes = Math.floor(duration / 60);
var seconds = (duration % 60);
if( seconds < 10 ) seconds = "0"+seconds;
var newRow = $("<tr></tr>");
newRow.append("<td>"+title+"</td>");
newRow.append("<td class='dur'>"+duration+"</td>");
$("tbody#videos").append(newRow);
});
newIndex = index+50;
$('#VideosLoaded').html(newIndex-1);
if(newIndex < max) {
getVideos(newIndex, max);
}
}
});
}
// Make table sortable (jquery.sortElements.js)
// via...