Ajax Combat: Managing multiple Event callbacks

Source: Internet
Author: User
Tags event listener

Before implementing our own solution, let's start with a simple example to understand the problem. Code Listing 4-7 shows a simple Web page in which a large div area responds to mouse movement events in two ways.

Code Listings 4-7 mousemat.html

First, it updates the state bar of the browser in the Writestatus () function, and then updates its image in the area by repositioning a point in the next small Thumbnail view area to replicate the movement of the mouse cursor position in the Drawthumbnail () function. Figure 4-6 shows the pages in the activity.

These two behaviors are independent of each other, and we want to be able to swap these behaviors with other responses to the mouse movement, even when the program is running.

Figure 4-6 the Mousemat program tracks mouse movement events in two ways in the "virtual Mousemat" main area: Updating the status bar below the browser with the mouse coordinates; the point where the mouse cursor is moved in the thumbnail view

The Mouseobserver () function is the event listener (by the way, the first line performs a bit of simple cross-browser magic.) Unlike Mozilla, Opera, or Safari, ie does not pass any arguments to the callback handler function, but instead saves the event object in Window.event. In this example, we call Writestatus () and Drawthumbnail () sequentially in the event handler function to make the two kinds of activity hard to join together. The program completes exactly what we want it to do, and because it's just a small program, the Code for Mouseobserver () is fairly clear. Ideally, we want to use a clearer way to connect event listeners so that they can be extended to more complex or dynamic situations.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.