Event listeners acting as infinite loop

When we add an event listener, the thing is even though it's only a single line and not within an infinite loop, it works like it is within an infinite loop though, because it keeps track of the event every time. Can someone explain what happens internally when we add an event listener please
9 Replies
Jochem
Jochem2mo ago
it's not acting like an infinite loop at all. addEventListener registers the callback you pass it to be fired whenever the event triggers. The javascript engine then calls the callback whenever the event occurs
Faker
FakerOP2mo ago
Hmm the thing is when we register the event, is there some kind of loops which continously check if the event has been fired? How does the browser know that a particular event has been fired?
Jochem
Jochem2mo ago
there's a bunch of ways that can work in the background. At the hardware level, it triggers an interrupt with can trigger code without needing a constant loop. I have no clue how it's actually implemented in the browser and javascript engine though. You really don't need to worry about the internals of the engine though
Faker
FakerOP2mo ago
Ok, I will not worry about that for now then, thanks !
ἔρως
ἔρως2mo ago
depend on the event, but you shouldnt have to even think about it unless you are trying to do something super super super outside the "norm" and what you should do by the way, some events will be more important than other events, and will execute before or even interrupt execution of other events
glutonium
glutonium2mo ago
ya it's just a browse thing, u don't really need to worry about it i assume the browser has a separate system where it keeps track of the events as they occur then simply checks if there's anything to be done upon that event being triggered, if so then ig the event handler handles that event , pushes the callback to the callstack this is just my assumption , that could be how the whole thing is working but yah there's really no reason to worry about these for the most part
ἔρως
ἔρως2mo ago
they are thrown into the execution queue as they arrive, sorted by priority
glutonium
glutonium2mo ago
ooh ya, i forgot that's a thing Traversy Media has a great playlist on JavaScript Under the hood. i watched it long ago. pretty cool to know
YouTube
Traversy Media
Traversy Media features the best online web development and programming tutorials for all of the latest web technologies from the building blocks of HTML, CSS & JavaScript to frontend frameworks like React and Vue to backend technologies like Node.js, Python and PHP
ἔρως
ἔρως2mo ago
it is pretty cool how js does the single-threat thing
Want results from more Discord servers?
Add your server