目前,我已经在我的站点www.midnightlisteners.com上集成了Last.fm应用程序接口,但它将所有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
});
});我的超文本标记语言最好在我的网站上看到: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
发布于 2012-09-04 16:33:58
这是一个一般性的问题。它是关于包含带有回调的异步调用的循环。循环会运行得非常快,并且会非常快地创建所有的$.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();
});
}
}https://stackoverflow.com/questions/12259388
复制相似问题