我列出了最近通过Tumblr API发布的Tumblr音频帖子,每个帖子都附有Last.fm API的艺术家摘要。我有正确的数据,但音频帖子没有按时间顺序显示。每次刷新页面时,它们都会以随机顺序出现。我想将最新的音频帖子显示在顶部。这是我的代码:
$(document).ready(function() {
var user = "nycxnc";
var key = "###";
var type = "audio";
var limit = "5";
var url = "https://api.tumblr.com/v2/blog/" + user +
".tumblr.com/posts/" + type + "?callback=?&filter=" + type +
"&limit=" + limit + "" + key + "";
$.getJSON(url, function(data) {
$.each(data.response.posts, function(i, item) {
var artist = item.artist;
var track = item.track_name;
var url2 =
"http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&track=" +
track + "&artist=" + artist +
"&api_key=###&autocorrect=1&format=json&callback=?";
$.getJSON(url2, function(data) {
$.each(data, function(i, item) {
artist_summary = item.bio.summary;
artist_image = item.image[4]["#text"];
$(".au-wrapper").append(
'<div class="audio-wrapper"><img src=' + artist_image +
'><div class="audio-meta"><span class="audio-title">' +
track + '</span><div class="audio-artist">' + artist +
'</div></div><div class="url">' + artist_summary +
'</div></div>');
});
});
});
});
});
如果第二个JSON url(Last.fm)依赖于来自Tumblr API的数据,如何以正确的顺序组合来自两个API的数据?我是Javascript的新手,我正在创建这个脚本作为学习实验,所以任何帮助都将非常感激。 (:谢谢。