首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Last.fm接口jQuery

Last.fm接口jQuery
EN

Stack Overflow用户
提问于 2012-09-04 16:18:25
回答 1查看 891关注 0票数 1

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

我想遍历所有,并将它们添加到相应的位置。此外,这将是伟大的,如果有人可以帮我得到小的艺术家图像也。

我的jQUery代码:

代码语言:javascript
复制
$(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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-04 16:33:58

这是一个一般性的问题。它是关于包含带有回调的异步调用的循环。循环会运行得非常快,并且会非常快地创建所有的$.getJSON()调用。到回调运行时,循环已经结束,因此回调的闭包作用域将只包含对上一个循环周期的数据的引用。

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

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

代码语言:javascript
复制
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();
        });
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12259388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档