Search code examples
javascriptalert

Why can't Javascript be paused like alert() does?


From what I've researched, Javascript can't be paused except by a few native functions like alert, confirm, and prompt. Why can't this functionality be used without the modal windows? It seems to me that interjecting a split-second pause would be very useful for avoiding execution timeouts.


Solution

  • In general, any function that does networking or uses timers to do things over a period of time will be asynchronous.

    If the function takes a callback, you can look at what the callback is used for and usually it will be obvious whether is is asynchronous or not. If the function does not offer a callback, then it has no way of communicating asynchronous results so it is probably not asynchronous.

    There's no ironclad way to tell for sure. It has to either be spelled out in the doc for a function or obvious from the way the interface works.

    Asynchronous operations are different under the covers than synchronous operations in that asynch operations have the notion of setting up an operation, starting the operation and then getting notified later of progress, completion or errors in the operation. Iterating an array is a synchronous operation. It has none of these issues. The code just runs synchronously. Issuing an ajax call consists of registering a callback for state notifications, then starting the ajax call, then continuing to run other javascript and then some time later, the callback is called with a state change on the ajax call (such as completion).

    REF: https://softwareengineering.stackexchange.com/questions/202047/what-determines-which-javascript-functions-are-blocking-vs-non-blocking