The array is being re-indexed when you do a .splice()
, which means you'll skip over an index when one is removed, and your cached .length
is obsolete.(执行.splice()
,该数组将重新索引,这意味着删除索引时将跳过索引,并且缓存的.length
已过时。)
To fix it, you'd either need to decrement i
after a .splice()
, or simply iterate in reverse...(要修复它,您需要在.splice()
之后减少i
,或者简单地反向迭代...)
var i = Auction.auctions.length
while (i--) {
...
if (...) {
Auction.auctions.splice(i, 1);
}
}
This way the re-indexing doesn't affect the next item in the iteration, since the indexing affects only the items from the current point to the end of the Array, and the next item in the iteration is lower than the current point.(这样,重新索引不会影响迭代中的下一个项目,因为索引仅影响从当前点到数组末尾的项目,并且迭代中的下一个项目低于当前点。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…