繁体   English   中英

如何在使用 setTimeout function 时停止循环

[英]how to stop loop while using setTimeout function

 const fruits = ['apple', 'mango', 'orange', 'date']; let found = 0; fruits.forEach((value, key) => { if (found == 0) { console.log("value", value) setTimeout(() => { if (value.length > 1) { found = 1; } }, 200) } })

当它被捕获时停止循环 found=1 但在使用setTimout后它的行为不像这样如何解决它

这是你想要的:

 const fruits = ['apple', 'mango', 'orange', 'date']; let found = 0; for (var i = 0; i < fruits.length; i++ ) { const value = fruits[i]; if (value.length > 1) { found = 1; console.log("Founded", value) break; } else { console.log("Value", value) } } console.log("Found: ",found)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM