3

When using alert or confirm in Javascript, the browser is forced into a synchronous process where everything (even the loading of another page) halts until the user dismisses the dialog.

I was wondering if there was any other way to force the browser to halt/pause using JS or jQuery.

I looked at similar questions such as:

How to force javascript function execute synchronously/sequentially?

How to make jQuery ui dialog box halt browser processing like an alert or confirmation box?

Getting synchronous behavior in javascript?

...but none of the answers seem to address the issue head on. If there is not a way to do it, I will fully accept that as an answer.

Community
  • 1
  • 1
Maxx
  • 3,925
  • 8
  • 33
  • 38
  • 1
    I think you mean "modal", not "synchronous". However, I think you need to explain your problem a bit more. Why do you want to prevent user interaction with the page? Are you using a custom dialog or something? – RB. Feb 13 '13 at 15:37
  • 3
    There is no way to do it in a modern browser without causing a "stop this script" dialog. – Kevin B Feb 13 '13 at 15:37
  • 2
    Why would you ever want to do this? Give us an example on how you intend to use this, and it gets a little easier! – adeneo Feb 13 '13 at 15:37
  • 2
    I wonder why, when so many developers have worked on ways to STOP JavaScript from freezing the browser, newer developers still ask us for ways to "enable" this again. – Blazemonger Feb 13 '13 at 15:40
  • 1
    Actually, even [`alert()` does not halt everything](http://stackoverflow.com/a/2734311/1048572) – Bergi Feb 13 '13 at 15:42
  • The specifics of the system I'm working with require me to hook into an existing UI process and catch a decision from the user within one function. Meaning the function is called and then the user input has to be captured and returned to the same function so that it can return it to the hook being used. In short, it works perfectly with "confirm()", but does not allow me to create a custom interface instead of the built in confirmation. – Maxx Feb 13 '13 at 15:43
  • 1
    This is what happens when you halt javascript: http://jsfiddle.net/THECC/embedded/result/ **(i wouldn't go to that link)** note how you can't click on ANYTHING. Your code asking the user to input something won't work if the browser is locked. – Kevin B Feb 13 '13 at 15:45
  • @Maxx What prevents you using a custom dialog? Are you opening the dialog on form submission or something? You have to be really, *really* clear in order for us to help you, becuase the question as asked is not sensible... – RB. Feb 13 '13 at 15:45
  • I should also add that this is a very specific and not ideal situation/setup. I'm working with a lot of functionality I have no access to change. Like I said, I suspect it can't be done without using confirm, and that's ok. I just wanted to double check. – Maxx Feb 13 '13 at 15:46
  • @RB yes, basically. for all intents and purposes, it's the same as if I was doing it on a form submit where i have no control over the form submit code itself. – Maxx Feb 13 '13 at 15:48

1 Answers1

2

No, there's currently no cross browser way to force an asynchronous function to execute synchronously.

However you may make use of continuations to write asynchronous code in a synchronous manner.

Read the following answer for more details: https://stackoverflow.com/a/14809354/783743

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • Whats an asynchronous function in JavaScript? – TomTom Feb 13 '13 at 15:42
  • Please answer, as I don't think there is such. Although some call using setTimeout() to make function asynchronous. – TomTom Feb 13 '13 at 15:45
  • 3
    @TomTom - JavaScript functions which do not execute in a logical order are asynchronous. For example AJAX calls are usually asynchronous since they return immediately instead of blocking the event loop until the response is received. – Aadit M Shah Feb 13 '13 at 15:47
  • Using AJAX could be called asynchronous, but functions in JavaScript are objects, and to my knowledge there is no way to define function asynchronous or synchronous. – TomTom Feb 13 '13 at 15:49
  • 1
    Javascript functions are synchronous. They may contain asynchronous calls, such as Ajax calls, but that doesn't make them asynchronous. – Reinstate Monica Cellio Feb 13 '13 at 15:49
  • I probably did mean "modal", I just didn't realize it had the same implications as using an alert... if it in fact does. – Maxx Feb 13 '13 at 15:50