jQuery.each 问题
是否可以运行 jQuery .each 语句一定次数,而不是针对每个正在迭代的项目?正在迭代的 JSON 是一个last.fm feed,当然,他们经常忽略“限制”请求。我想通过仅运行 .each 语句多次来绕过此错误。
Is it possible to run a jQuery .each statement a certain amount of times rather then for each of the items being iterated? The JSON being iterated is a last.fm feed, and of course, they ignore the "limit" request often. I would like to bypass this error by only running the .each statement so many times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(5)
或者
or
不要修改 .each,而是修改选择器以仅选择前“n”个元素。
http://docs.jquery.com/Selectors/lt#index
Rather than modify the .each, modify your selector to select only the first 'n' elements.
http://docs.jquery.com/Selectors/lt#index
最好的解决方案是使用 jQuery 中的 .slice 方法。
The best solution is to use the .slice method in jQuery.
在循环之前定义一个变量,在每个循环中递增它,然后
if(myvar == 10){return false;}
Define a variable before your loop, increment it within the each, and
if(myvar == 10){return false;}
当您需要时,从迭代器中返回
false
停止。迭代器的第一个参数是索引(循环数组时)。Return
false
out of the iterator when you want to stop. The first parameter to your iterator is the index (when looping through arrays).