Java Reference
In-Depth Information
Event Listeners
Imagine that you're waiting for a really important email message that you need to act upon
as soon as it arrives, but you also have some JavaScript programming to do (you're working
on the next big killer app). You could keep checking your email every couple of minutes to
see if the important message has arrived, but this will cause lots of interruptions to your ninja
training. Not to mention that you might be unable to check your email at the exact moment
the message arrives, so it might be too late to act upon. The obvious answer to this dilemma
is to set up a notification that will pop up as soon as the email arrives. You can happily pro-
gram away, without being distracted by having to check your email.
Event listeners in JavaScript work in much the same way. They are like setting a notification
to alert you when something happens. Instead of the program having to constantly check to
see if an event has occurred, the event listener will let it know when the event happens and
the program can then respond appropriately. The program can get on with the tasks while it
waits for the event to happen.
For example, say in your program you want something to happen when a user clicks on
the page. The code to check if a user has clicked might look like this example (JavaScript
doesn't actually work like this, so this code would fail to work, although it is the way some
applications work):
if (click) {
} else {
// carry on with rest of the program
The problem with this approach is that the program would have to keep returning to this if
block to check if the click had happened. It's a bit like having to check your email every few
minutes. This is known as a blocking approach to programming, as checking for the click is
blocking the rest of the program from running.
Search WWH ::

Custom Search