forEach 用 break和return false都是无用的。break会报错
那怎么办?就是用try-catch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
try { var array = ["first","second","third","fourth"]; // 执行到第3次,结束循环 array.forEach(function(item,index){ if (item == "third") { throw new Error("EndIterative"); } alert(item);// first,sencond }); } catch(e) { if(e.message!="EndIterative") throw e; }; // 下面的代码不影响继续执行 alert(10); |