How to terminate endless while loop via setTimeout in Javascript
问题 I have a piece of code with while loop which I would like to stop by setTimeout(). But it seems like a endless loop, which never triggers setTimeout(). If I remove while loop, timeout triggers correctly. What is wrong please? $(document).ready(function() { var i = 0, s = false; setTimeout( function() { s = true; console.log( "Timeuot!!!" ); console.log( "s value is " + s ); }, 1000 ); while( s === false ) { console.log( "this is while and s is " + s ); i++; } console.log( "iterations: " + i )