Stop JavaScript execution without blocking the browser

Can you stop JavaScript without blocking the browser? The way you usually stop execution is to do an endless while() -loop, but in the case of FireFox, it blocks the browser until the loop ends.

What are you doing this?





I am trying to override window.confirm() to implement my own dialog using HTML. I do this, so I do not need to change the existing code (this is a fairly large code base).

I need to be able to stop execution so that user input; in turn, returns a logical value similar to the standard confirmation function:

 if (confirm("...")) { // user pressed "OK" } else { // user pressed "Cancel" } 




Update

As far as I know; this cannot be done with setTimeout() or setInterval() , because these functions execute the asynchronous code assigned to them.

+6
javascript execution
source share
7 answers

confirm() prompt() and alert() are special functions - they call JavaScript from the sandbox in the browser, and the browser pauses JavaScript. You cannot do the same, since you need to create your functionality in JavaScript.

I do not think that there is a great way to change the replacement without performing some restructuring in the following areas:

 myconfirmfunction(function() { /* OK callback */ }, function() { /* cancel callback */ }); 
+8
source share

You cannot stop the event flow in JavaScript, so instead you have to work around the problem, usually using callback functions. These are functions that are launched later, but can be passed like any other object in JavaScript. You may be familiar with them from AJAX programming. So for example:

 doSomeThing(); var result = confirm("some importart question"); doSomeThingElse(result); 

Will be converted to:

 doSomeThing(); customConfirm("some importart question", function(result){ doSomeThingElse(result); }); 

where customConfirm now takes the question and passes the result to the function that it takes as an argument. If you implement the DOM dialog using a button, connect the event listener to the OK and CANCEL buttons and call the callback function when the user clicks on one of them.

+7
source share

Either use callbacks, or create your code only for Firefox. In Firefox with JavaScript 1.7 and higher, you can use the yield statement to simulate the desired effect. I created the async.js library for this purpose. The standard library for async.js includes a confirmation method that can be used as such:

 if (yield to.confirm("...")) { // user pressed OK } else { // user pressed Cancel } 
+7
source share

There is an extension for the JavaScript language called StratifiedJS. It runs in every browser, and it allows you to do just that: stopping one line of JavaScript code without freezing the browser.

You can enable Stratified JavaScript, for example. by enabling Oni Apollo ( http://onilabs.com/docs ) on your web page, for example:

 <script src="http://code.onilabs.com/latest/oni-apollo.js"></script> <script type="text/sjs"> your StratifiedJS code here </script> 

Your code will look like this:

 var dom = require("dom"); displayYourHtmlDialog(); waitfor { dom.waitforEvent("okbutton", "click"); // do something when the user pressed OK } or { dom.waitforEvent("cancelbutton", "click"); } hideYourHtmlDialog(); // go on with your application 
+3
source share

the way you usually stop execution is unlikely to be an endless while loop.

decompose your work into parts that you call with SetTimeout

change this:

  DoSomeWork(); Wait(1000); var a = DoSomeMoreWork(); Wait(1000); DoEvenMoreWork(a); 

:

  DoSomeWork(); setTimeout(function() { var a = DoSomeMoreWork(); setTimeout(function() { DoEvenMoreWork(a); }, 1000); }, 1000); 
+2
source share

I don’t think there is a way to intelligently recreate the confirm () or prompt () functionality in your own JavaScript. They are "special" in the sense that they are implemented as calls to the browser’s native library. You really can't make this type of modal dialog in JavaScript.

I have seen various user interface libraries that mimic the effect of placing an element on top of a page that looks and acts like a modal dialog, but they are implemented using asynchronous callbacks.

You will have to modify the existing library, and not replace window.confirm.

+1
source share

I tried using a tight loop for this . I needed to slow down my own event (which AFAIK is the only precedent for synchronous waiting, which cannot be re-archived asynchronously). There are many examples of loops that claim to not block the browser; but none of them worked for me (the browser did not lock, but they prevented him from doing what I expected in the first place), so I abandoned this idea.

Then I tried this - to save and play back an event that also seems impossible for a cross browser. However, depending on the event and how flexible you should be, you may come closer.

In the end, I gave up and felt much better; I found a way to make my code work without having to slow down my own event at all.

0
source share

All Articles