While loop js4/30/2023 Instead, an event gets put in the Javascript event queue and then, when the currently running Javascript finishes, the next event is pulled from the event queue and it gets its turn to run. So, when a timer fires or a network request arrives, it does not ever "interrupt" the currently running Javascript. Then, only when it returns back to the interpreter, Javascript gets the next event from the event queue and runs it.Īll things like timers and network events run through the event queue. That means that it runs a piece of Javascript until it returns control back to the interpreter. Will simply run forever (or until the browser complains about a non-responsive javascript loop), the page will appear to be hung and no other javascript will ever get a chance to run, thus the flag's value can never be changed by code outside this loop.įor a little more explanation, Javascript is an event driven language. You created an infinite loop where the flag value that terminates the loop can never be changed by code outside this loop because no code outside the loop ever gets to run - thus this will never work.īecause javascript in a browser is single threaded (except for webworkers which aren't involved here) and one thread of javascript execution runs to completion before another can run, your statement: // this won't work!
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |