# Any way to make scripts 'responsive' and not just freeze the page/execute the whole script?

**URL:** https://forum.eternagame.org/t/any-way-to-make-scripts-responsive-and-not-just-freeze-the-page-execute-the-whole-script/2463
**Category:** Tools
**Created:** [August 26, 2015, 7:55pm UTC](https://forum.eternagame.org/t/any-way-to-make-scripts-responsive-and-not-just-freeze-the-page-execute-the-whole-script/2463 "2015-08-26T19:55:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LFP6](https://forum.eternagame.org/user_avatar/forum.eternagame.org/lfp6/32/7_2.png) [@LFP6](https://forum.eternagame.org/u/LFP6)
#### Post date: [August 26, 2015, 7:55pm UTC](https://forum.eternagame.org/t/any-way-to-make-scripts-responsive-and-not-just-freeze-the-page-execute-the-whole-script/2463/1 "2015-08-26T19:55:00Z")

</div>

I’d like to see the ability to make much more user-friendly UIs, but it’s quite limited when you hit the button and the page freezes while the whole script executes. First off, the page should be allowed to update (progress bar!), and also there should be an easy way to ask the user for continued input (IE, a custom browser could be created).

I’d do some looking at this myself outside of EteRNA specifically, but I honestly don’t know how it would work with how the interface might make the situation different from normal page scripting.

---

<div class="post-metadata">

### Author: ![Omei](https://forum.eternagame.org/user_avatar/forum.eternagame.org/omei/32/4420_2.png) [@Omei](https://forum.eternagame.org/u/Omei)
#### Post date: [August 26, 2015, 9:42pm UTC](https://forum.eternagame.org/t/any-way-to-make-scripts-responsive-and-not-just-freeze-the-page-execute-the-whole-script/2463/2 "2015-08-26T21:42:00Z")

</div>

The Eterna scripting environment&nbsp;\_is&nbsp;\_a Web page. &nbsp;Web pages don’t _ask_ for input from the user, they _respond_ to input from the user. &nbsp;So if you want an interactive Web page, you add event handlers to handle (i.e. execute code in response to) user input.

A trivial example: copy and paste the following script into the code window and click on Evaluate.

```
document.body.onclick = function() {outln("Click")};

```

The script will run and complete immediately. &nbsp;But now each time you click on the page, a new line of “Click” will be added to the output window.&nbsp;

---

<div class="post-metadata">

### Author: ![LFP6](https://forum.eternagame.org/user_avatar/forum.eternagame.org/lfp6/32/7_2.png) [@LFP6](https://forum.eternagame.org/u/LFP6)
#### Post date: [August 26, 2015, 10:30pm UTC](https://forum.eternagame.org/t/any-way-to-make-scripts-responsive-and-not-just-freeze-the-page-execute-the-whole-script/2463/4 "2015-08-26T22:30:00Z")

</div>

Right, right. But how would I prevent the page from becoming completely unresponsive while executing? I’m not even sure what the cause is of that.

---

<div class="post-metadata">

### Author: ![Omei](https://forum.eternagame.org/user_avatar/forum.eternagame.org/omei/32/4420_2.png) [@Omei](https://forum.eternagame.org/u/Omei)
#### Post date: [August 26, 2015, 11:03pm UTC](https://forum.eternagame.org/t/any-way-to-make-scripts-responsive-and-not-just-freeze-the-page-execute-the-whole-script/2463/6 "2015-08-26T23:03:00Z")

</div>

OK, I think I understand your question now.

Javascript is inherently single-threaded; that’s why there is no need for synchronization primitives in the language. &nbsp;But that doesn’t mean you can’t get the end result you are looking for.

The standard method used to be to break up the long-running work into smaller pieces. &nbsp;Each piece would

1. Do some useful work, then
2. Create a timer event that would kick off the next piece in the near future.  
This works because the Javascript interpreter can process any pending user input events before the timer kicks off and starts the next piece of the long computation.

In HTML 5, there is a concept of Web Workers. &nbsp;A Web Worker is implemented essentially by starting up a new Javascript interpreter instance. &nbsp; The main program can get results from the Web Worker by creating event handlers to respond to messages sent by the Web Worker. &nbsp;

I did a quick check to see if the Eternascript environment supported Web Workers by running

```
outln (Worker)

```

and got back

```
function Worker() { [native code] }

```

so it appears, on the surface, to support them.

---

<div class="post-metadata">

### Author: ![LFP6](https://forum.eternagame.org/user_avatar/forum.eternagame.org/lfp6/32/7_2.png) [@LFP6](https://forum.eternagame.org/u/LFP6)
#### Post date: [August 27, 2015, 1:25am UTC](https://forum.eternagame.org/t/any-way-to-make-scripts-responsive-and-not-just-freeze-the-page-execute-the-whole-script/2463/8 "2015-08-27T01:25:00Z")

</div>

Awesome, thank you!
