gpt4 book ai didi

html - Last.fm API jQuery

转载 作者:行者123 更新时间:2023-12-01 08:13:08 24 4
gpt4 key购买 nike

目前我已在我的网站上集成了 Last.fm API www.midnightlisteners.com但它将所有 Last.fm 数据都放在最后一个 Kanye West 上。如果将鼠标悬停在 ( i ) 图标上,您将看到工具提示中出现数据。

我想循环遍历所有并将它们添加到相应的位置。另外,如果有人也能帮我获取小艺术家图像,那就太好了。

我的 jQUery 代码:

$(document).ready(function() {
// Find Related Artists based on Last.fm JSON Results
$(".artist-data").each(function() {
// Find the artist name in the "p" tag and save it
artistName = $(this).find(".artist-wrap-mid p");
artist = artistName.text();
// Create a class out of the artist name made of lowercase letters and "-" instead of spaces
artistClass = artist.toLowerCase().replace(/ /g, '-');
// Add this class to the .artist-data div
$(this).addClass(artistClass);

// Check if a value is present
if (artist === '') {
$("." + artistClass + " .related").html("No related artist info found for " + artist);
}
// Otherwise return the request with links to each related artist
else {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=" + artist + "&api_key=9c991c10bf461ac4e4f92fdfa14c20c2&limit=3&format=json&callback=?", function(data) {
var html = '';
$.each(data.similarartists.artist, function(i, item) {
html += "<a href='http://" + item.url + "' target='_blank'>" + item.name + "</a>, ";
}); // End each
$("." + artistClass + " .related").append(html);
}); // End getJSON
} // End Else
});
});

我的 HTML 最好在我的网站中查看:www.midnightlisteners.com

但它将 Last.fm 中的所有数据放入 <div class="related"> </div>

我在这里得到了大量的帮助:writing.sackettsolutions.com/2012/02/navigating-the-last-fm-api-with-a-little-help-from-jquery-getjson

最佳答案

这是一个普遍问题。它是关于包含带有回调的异步调用的循环。该循环将运行得非常快,并且将非常快地创建所有 $.getJSON() 调用。当回调运行时,循环已经完成,因此回调的闭包范围将仅包含对最后一个循环周期的数据的引用。

解决方案:松开循环...仅在前一个循环完成回调后才开始下一个循环。因此,您不必运行固定的 .each() 循环,而是必须增加回调内的索引并“手动”启动下一个循环周期。

编辑2:您的代码应该是(未经测试!)

var currIndex = 0;
var $currArtists = $('.artist-data');

if($currArtists.length > 0) getNextArtistInfo();

function getNextArtistInfo() {
// get reference to current artist
var $currArtist = $currArtists.eq(currIndex);
artistName = $currArtist.find(".artist-wrap-mid p");
artist = artistName.text();
// Create a class out of the artist name made of lowercase letters and "-" instead of spaces
artistClass = artist.toLowerCase().replace(/ /g, '-');
// Add this class to the .artist-data div
$currArtist.addClass(artistClass);

// Check if a value is present
if (artist === '') {
$("." + artistClass + " .related").html("No related artist info found for " + artist);
currIndex++;
if(currIndex < $currArtists.length)
getNextArtistInfo();
}
// Otherwise return the request with links to each related artist
else {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=" + artist + "&api_key=9c991c10bf461ac4e4f92fdfa14c20c2&limit=3&format=json&callback=?", function(data) {
var html = '';
$.each(data.similarartists.artist, function(i, item) {
html += "<a href='http://" + item.url + "' target='_blank'>" + item.name + "</a>, ";
}); // End each
$("." + artistClass + " .related").append(html);
currIndex++;
if(currIndex < $currArtists.length)
getNextArtistInfo();
});
}
}

关于html - Last.fm API jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12259388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com