execution order of actions

M

Matt Kruse

Is this related to execution order - or something else?

Something else.

The document will not refresh itself (and show the div) until the javascript
execution ends.
If you want the notification div to display, you should run a function to
display it, and have that function call setTimeout to run another function
in, say, 10ms. Then the function will end, your change will be shown in the
browser, and the other function to do the real work will fire after 10ms.
Then, at the end of that function, hide the div again.
 
W

web.dev

I have a problem i'm thinking might be related to execution order as
discussed in this thread. Save the code below as an html file, load it
in your browser, and click the link to run the test.

The idea is that i show a hidden DIV while a potentially long-running
function runs alerting the user that something is happening. Once the
function is done - the DIV is hidden again.

However, what happens is that the function runs - but the DIV never
displays.

But......if you un-comment out the window.alert statement - the DIV
will display.

Is this related to execution order - or something else?



<!-----test.html------>
<a href="javascript:jsFunction()">Run Test</a>

<div id="notification" style="display: none; background: black; font:
white"></div>

<script>
function jsFunction() {
document.getElementById("notification").innerHTML = "test
notification";
document.getElementById("notification").style.display = "";
//window.alert("test");
var x = 0;
while (x<2099999) {
x++;
}
document.getElementById("notification").innerHTML = ""
document.getElementById("notification").style.display = "none";
}
</script>
<!--end-->

This is how it is being run. Initially your div is hidden. Then when
you call your jsFunction the following will happen. If alert is
uncommented, then your div will be shown, and all execution will be
blocked until you hit "Ok" on the alert. That is why you are able to
see it. However, when the alert is commented, then your div is shown
AND hidden during the same execution scope regardless of how big your
while loop is. That is why you are not able to see the div being
shown, since you are hiding it again.
 
V

VK

I have a problem i'm thinking might be related to execution order as
discussed in this thread.
<!-----test.html------>
<a href="javascript:jsFunction()">Run Test</a>

<div id="notification" style="display: none; background: black; font:
white"></div>

<script>
function jsFunction() {
document.getElementById("notification").innerHTML = "test
notification";
document.getElementById("notification").style.display = "";
//window.alert("test");
var x = 0;
while (x<2099999) {
x++;
}
document.getElementById("notification").innerHTML = ""
document.getElementById("notification").style.display = "none";
}
</script>
<!--end-->

It is not connected with the execution order per se but it is connected
with *synchronized* methods (more exactly - with the absence of such)
in the standard ECMAScript.
Here we have two independent system: script context and browser
rendering engine. Script cannot directly manipulate the rendering
engine - it just places requests to change rendering options (like
style.display) and continues its execution. If immediately after that
the script goes in some overly intensive process (like your loop), the
rendering engine never gets enough time to refresh the page so it just
holds on that until the script will get less demanding for resources.
Your alert("Test") simply gives to the rendering engine the needed
micro-break to accomplish the pending refresh request.
As script and rendering engine are two independent system, my
pseudo-synchronization trick with bogus var will not work:

var foo = (document.getElementById("notif").style.display = "block");
// doesn't work

foo will immediately get its undefined value ("request to redraw the
page is being sent successbully") and script will continue.

You need the full pseudo-sync in your case. Something like:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<a href="javascript:void(jsFunction())">Run Test</a>


<div id="notif" style="display: none; background-color: black;
color:white"></div>


<script>
function jsFunction() {
syncFun1();
setTimeout('syncFun2()',500);
}

function syncFun1() {
document.getElementById("notif").style.display = "block";
document.getElementById("notif").innerHTML = "test notification";
}

function syncFun2() {
var x = 0;
while (x<2099999) {
x++;
}
syncFun3();
}

function syncFun3() {
document.getElementById("notif").innerHTML = "";
document.getElementById("notif").style.display = "none";
}
</script>

</body>
</html>
 
A

aundro

Matt Kruse said:
Something else.

The document will not refresh itself (and show the div) until the javascript
execution ends.
If you want the notification div to display, you should run a function to
display it, and have that function call setTimeout to run another function
in, say, 10ms. Then the function will end, your change will be shown in the
browser, and the other function to do the real work will fire after 10ms.
Then, at the end of that function, hide the div again.

Thank you for this information, as that is a topic I too was
wondering about lately ("when does the browser actually refresh?").

Arnaud
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top