04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 18: Advanced Techniques<br />

Timers can be created in JavaScript using setTimeout() or setInterval()as follows:<br />

❑<br />

❑<br />

❑<br />

Timer code is placed into a holding area until the interval has been reached, at which point the<br />

code is added to the JavaScript process queue to be executed the next time the JavaScript process<br />

is idle.<br />

Every time a piece of code executes completely, there is a brief amount of idle time to allow<br />

other browser processes to complete.<br />

This behavior means that timers can be used to split up long - running scripts into smaller chunks<br />

that can be executed at a later time. Doing so helps the web application to be more responsive to<br />

user interaction.<br />

The observer pattern is used quite often in JavaScript in the form of events. Although events are used<br />

frequently with the DOM, they can also be used in your own code by implementing custom events. Using<br />

custom events helps to decouple different parts of code from one another, allowing easier maintenance<br />

and reducing the chances of introducing an error by changing what seems to be isolated code.<br />

Drag-and-drop is a popular user-interface paradigm for both desktop and web applications, allowing<br />

users to easily rearrange or configure things in an intuitive way. This type of functionality can be created<br />

in JavaScript using mouse events and some simple calculations. Combining drag-and-drop behavior<br />

with custom events creates a reusable framework that can be applied in many different ways.<br />

616

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!