-1

I'm rather new to Javascript and some of the syntax is not very clear to me. I've usually copied example code from the internet when I needed it, but I would like to understand it better.

Can someone explain, in simple terms, how javascript knows to call the function supplied, and how it knows to do it asynchronously? Is there something that tells it do do this, or is it just built into the language?

Thanks

L_7337
  • 2,650
  • 28
  • 42

1 Answers1

0

Whenever you want to something when something happens (and that something is outside the control of JavaScript (e.g. "When the user clicks on a button" or "When the HTTP response is received from the network".)) you typically use an Event Listener.

This is a function that you tell JavaScript to run when the event happens.

They are typically set up by using the addEventListener method or by assigning a function to a property with a predefined name on the right kind of object.

When the event happens some code (typically (in a browser this is almost always) provided by the underlying environment) will check to see if any appropriate event listener functions exist and executes them.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335