Ell*_*ary 6 javascript jquery json soundcloud
所以我正在使用soundcloud API来吸引用户的收藏.它们的最大限制是每个请求200个,但是在对象的最后,它们有一个a_href键,其值是下一页的收藏夹.
基本上,我正在尝试放置一个按钮,以便用户可以点击它,它将为他们提供接下来的200个喜欢.我的问题是无需多次调用原始API URL即可访问data.a_href.我的代码看起来像这样:
function getAPIURL(username, subSection){
apiurl = "https://api.soundcloud.com/users/" + username + "/" + subSection + "/?client_id=" + clientID + limit + "&linked_partitioning=1"
}
function getFavorites(){
titleList.length = 0;
artistList.length = 0;
imgList.length = 0;
idList.length = 0;
$(".trackList").html("");
username = $("input").val();
subSection = "favorites";
getAPIURL(username, subSection);
getAPI(apiurl);
}
function getAPI(apiurl){
$.getJSON(apiurl, function(data) {
//Does some stuff then
$(".nextPage").on("click", function(){
titleList.length = 0;
artistList.length = 0;
imgList.length = 0;
idList.length = 0;
$(".trackList").empty();
getAPI(data.next_href);
})
});
}
Run Code Online (Sandbox Code Playgroud)
当我进入第二页时,上面的工作很棒.然而,当我尝试去第三页时,就像它在调用第三页之前调用第二页和第一页一样....
有任何想法吗?
这是在每次调用时重新应用点击:
function getAPI(apiurl){
$.getJSON(apiurl, function(data) {
//Does some stuff then
$(".nextPage").on("click", function(){
titleList.length = 0;
artistList.length = 0;
imgList.length = 0;
idList.length = 0;
$(".trackList").empty();
getAPI(data.next_href);
})
});
}
Run Code Online (Sandbox Code Playgroud)
每次调用时,单击处理程序的数量都会增加 1,并且每个处理程序都会被调用。
要停止重新涂抹,请将其取下然后涂抹。off()进行删除。
function getAPI(apiurl){
$.getJSON(apiurl, function(data) {
//Does some stuff then
$(".nextPage").off("click").on("click", function(){
titleList.length = 0;
artistList.length = 0;
imgList.length = 0;
idList.length = 0;
$(".trackList").empty();
getAPI(data.next_href);
})
});
}
Run Code Online (Sandbox Code Playgroud)
为了让以后的生活更轻松,请将绑定onclick与getAPI调用分开。
如果在某些事件发生之前按钮不存在于 DOM 中,则绑定到父元素,并且它们将不需要off/ on:
$("PARENT OF THE .nextPage BUTTON").on("click", ".nextPage", function(){
...
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
95 次 |
| 最近记录: |