我正在制作一个名为“Your Life in Music”的应用程序,用户在其中插入他/她的生日,该应用程序会显示自他们出生以来每个生日的热门广告牌。该应用程序目前非常缓慢,因为从 Last.fm api 检索所有专辑封面图像需要一段时间。我想知道如何使用 JS/jQuery 加速 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];
try {
var track = billboard[date]['song'];
} catch (err) {
console.log(date)
}
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=6ad5bad28ffc4dedbcf1115b3ddf5273&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)
})
}