M
M
McBernar2018-02-28 17:57:40
JavaScript
McBernar, 2018-02-28 17:57:40

Why is the code running in parallel?

Hello.
Baby, a question.
There is a js code (pseudo):

while (elem.opacity > 0) {
    elem.opacity -= .1;
}
elem.remove();

It's simple - we change the transparency of the DOM element in a loop from 1 to 0 in increments of 0.1.
After that, we remove the DOM element.
So the question is - why does the removal occur before the opacity becomes 0?
Why do these processes occur not sequentially, but in parallel? After all, in theory, the interpreter should wait until the end of the loop and only after that continue executing the code, but in reality the change in opacity is not visible, but it is clear that the DOM element is deleted immediately upon loading.
Where to dig to understand the essence of what is happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-28
@McBernar

In your case, most likely, opacity changes in thousandths of a second and remove works .
Demo.
All execution steps in the console. On my work machine, opacity changes from 1 to 0 in 0.005 seconds. Imagine how little that is.
The code is synchronous. It cannot run in parallel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question