我正在制作一个名为“Your Life in Music”的网络应用程序,用户在其中插入他/她的出生日期,该应用程序会显示自他们出生以来每个生日的热门广告牌。该应用程序与 Last.fm api 对话以检索每首歌曲的专辑封面。目前,该应用程序需要一段时间来编译所有专辑封面,有人建议通过将所有 API 调用组合到一个请求中来加快速度。不过,我有点新手,我不确定这是否可以做到。所以我的问题是:如何将多个调用组合成一个请求?
这是应用程序:http ://www.dailyspiro.com
和关键代码:
function compileDisplay(birthDays) {
// create an HTML list that for each birthday displays top song, artist, and album art
htmlObj = {}
for (i = 0; i < birthDays.length; i++) {
var age = birthDays[i][0]; // age isn't equal to i if user was born before 1958
var date = birthDays[i][1];
var track = billboard[date]['song'];
var artist = billboard[date]['artist'];
getAlbumArt(age, track, artist, birthDays);
}
}
function getAlbumArt(age, track, artist, birthDays) {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=(myApiKey)&artist=" + artist + "&track=" + track + "&format=json&callback=?", function (data) {
try {
var image = data.track.album.image[2]['#text'];
if (image == 'http://cdn.last.fm/flatness/catalogue/noimage/2/default_album_medium.png') {
throw 'generic image';
}
} catch (err) {
var image = 'images/' + track + '.jpg';
}
compileHtml(age, track, artist, image, birthDays)
})
}