【发布时间】:2014-06-26 00:39:31
【问题描述】:
我制作了一个 JSON 文件来存储 SoundCloud URL。然后我的$.each() 加载 JSON 文件的数据,并根据数据从 SoundCloud API 收集更多数据。不知何故,因为 SoundCloud 导致我的循环以随机顺序加载数据,而不是 JSON 文件中的方式。
这是我获取数据和 SoundCloud 位的代码:
$.getJSON("http://www.json-generator.com/api/json/get/bLjOHIYsAy?indent=2", function(data){ //Link of the playlist
$.each(data.PlayListArray, function(key, val){ //navigate to array called PlayListArray
var songLink = val.URL; // the value of URL in the array
SC.get('/resolve', { url: songLink }, function(track) { //convert SoundCloud URLs to ids to get their data.
$("#demo").append("<p id= "+ track.id + ">" + track.title + "</p>");
});
});
});
当我在循环中没有任何 SoundCloud 代码的情况下加载数据时,顺序是正确的。但是每次我刷新页面时,soundcloud 的顺序都是不同的。请看演示:http://jsfiddle.net/Fq2Rw/
在 JSFiddle 上,请多次运行代码,每次都会看到顺序变化。这是为什么?任何的想法?或者有什么不同的方式来循环 JSON?
【问题讨论】:
标签: javascript jquery json loops soundcloud