how do an infinite loop in javascript
问题 Im trying to do an infinite loop using while between 0 to 100 and 100 to 0, but the browser crashes. There is a way to clear the browser memory? This is my code: var a = 0; var flag = true; while (true) { if (a < 100 && flag == true) { a++; } else { a = 0; flag = false; if (a < 0) { flag = true; } } console.log(a); } 回答1: An infinite while loop will block the main thread wich is equivalent to a crash. You could use a selfcalling function ( wich leaves the main thread doing some other stuff