【问题标题】:Last.fm API jQueryLast.fm API jQuery
【发布时间】:2012-09-04 21:12:13
【问题描述】:

目前我已经在我的网站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 中的所有数据放入 &lt;div class="related"&gt; &lt;/div&gt;

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

【问题讨论】:

    标签: html jquery last.fm


    【解决方案1】:

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

    【讨论】:

    • 谢谢,你能帮我解决这个问题吗?我对 jQuery 真的很陌生。我刚开始阅读 jQuery 新手到忍者书:D
    • 是的,它工作得很好!! :) 非常感谢 Devnull。我不知道,但如果你有时间,你可以帮助艺术家图片吗?他们在这里使用它们,但我不知道如何在我的页面上实现它。 Link
    • 您需要什么“艺术家图片方面的帮助”??
    • 哦,对不起,我没有解释自己。但我现在把它们加载了:) 再次感谢你的所有帮助。
    • 我确实改变了:"&lt;a href='http://" + item.url + "' target='_blank'&gt;" + item.name + "&lt;/a&gt;, "; TO "&lt;li&gt; &lt;a href='http://" + item.url + "'&gt;&lt;img src='" + item.image[1]['#text'] + " ' alt=' " + item.name + " '/&gt;" + item.name + "&lt;/a&gt; &lt;/li&gt;"; 它现在也从 Last.fm 加载图像
    猜你喜欢
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2023-04-04
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多