1

目前我已经在我的网站www.midnightlisteners.com上集成了 Last.fm API,但它会将所有 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

4

1 回答 1

3

这是一个普遍的问题。它是关于包含带有回调的异步调用的循环。循环将运行得非常快,并且会非常快地创建所有 $.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();
        });
    }
}
于 2012-09-04T08:33:57.563 回答