jQuery 3 - Using Events |
Written by Ian Elliot | ||||||||
Monday, 01 May 2017 | ||||||||
Page 4 of 4
Events and More EventsIn this chapter we have covered the events that jQuery deems important enough to have their own functions. This is a relatively small set of the events that a typical browser support. For example, the MDN Event Reference has a very long list of standard events and an equally long list of non-standard events. You can use any of these events via jQuery using the on, one and off functions to attach handlers. You might think that you are safe to use the standard events but all you can assume is a reasonable expectation that modern browsers will have implemented them. What you cannot assume is that they have implemented them in the same way. Small differences in implementation often cause big problems and unfortunately jQuery offers no help in smoothing over browser differences for the vast majority of events. For example the wheel event is the standard for detecting when a mouse wheel has been rotated. You can use it something like:
Notice that to access the deltaY property we need to use the originalEvent object because jQuery doesn't unpack this into its event object. The deltaY is negative and positive according to which way the wheel has been rotated. This is easy but until recently the wheel event didn't work with Safari or with Edge. Finding out which browsers support which events isn't easy and finding out exactly how they support them is even harder. In practice all you can reasonably do is to try your program out on the browsers that you need to support. Immediate EventsSo far the key idea in understanding JavaScript and even handling has been the idea that JavaScript has only one thread of execution (unless you create others using web workers say). What this means for events is that only one event handler can be active at any given moment. If another event occurs while an event handler or any JavaScript is running then it is simply added to the event queue and it gets its chance to run when the currently executing code stops running and frees the thread. This is sometimes explained as "run to completion" in the sense that any JavaScript that is running runs until it is completed and isn't interrupted by the system to do something else. This really is the big idea but like all principles there are minor exceptions and these aren't well documented or particularly well thought out. Consider for a moment the following program:
Two buttons each with their own click event handlers that display messages on the console. The complication is that the first button click handler fires the click event on the second button. What do you see on the event log? By the "run to completion" rule the first button's event handler should fire the click event on button2, which should be added to the even queue to all the currently executing event handler to complete. So you should see button click 1 followed by button click 2. You don't - you actually see button click 2 followed by button click 1. When you fire an event in code it behaves more like a function call. Indeed this is exactly what is happening when you fire an event in software all that happens is that the functions that are stored in the list of event handlers are called one after another. There is still only a single thread of execution but it transfers to the other event handler before completing the current event handler. This is something we will have to return to when we look at custom events in the next chapter. There are also example where an event handler is called immediately when a real not a just a software event occurs but these are rare, very browser and even OS dependent. The one that is usually quoted is the firing of a resize event while an Alert is shown on the screen. The important point is that JavaScript is single-threaded and only one thing happens at a time, but the rules for how control is passed from one event handler to another can be more complicated than you might expect.
Summary
More InformationJust jQuery
|
JavaScript Canvas - Fetch API Working with lower-level data is very much part of graphics. This extract from Ian Elliot's book on JavaScript Graphics looks at how to use typed arrays to access graphic data. |
JavaScript Jems - The Inheritance Tax JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way. In particular, it doesn't do inheritance [ ... ] |
Other Articles |
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info