A
A
Andrew2017-08-16 00:06:53
JavaScript
Andrew, 2017-08-16 00:06:53

Where exactly are asynchronous event handlers registered?

in articles about the event loop they write that event handlers are registered in the environment, but they don’t really explain and don’t go deep, as I understand it in my case (Web) this means in the browser, but where in the browser? if there is one thread in js, then how does setTimeout count time in parallel with execution before adding it to the queue?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2017-08-16
@Stalker_RED

setTimeout doesn't count anything, it adds the task to the scheduler.
Did you read this? https://learn.javascript.ru/settimeout-setinterval
More about event loop

L
Leo Developer, 2017-08-16
@crazy_leo

For example here is the code:

console.log("Hello world")
setTimeout(() => console.log("Hello timeout"), 50)
console.log("Hello JS")

Eventloop is given the task to execute a script.
The interpreter executes the first line, then the second.
Ie the timer, the second task is created. At the moment there are 2 of them.
1) Run the script
2) Call the timer no earlier than 50 ms.
All tasks are performed in turn.
Therefore, first you need the script to be fully executed.
That is, hello js is also displayed.
This task is removed and the eventloop looks to see if it has more tasks.
Then the second task is performed.
Eventually:
console.log("Hello world")
console.log("Hello JS")
console.log("Hello timeout")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question