liveBook · Manning

Chapter 13: Chapter 13. Surviving events

What you will learn in chapter 13 of Secrets of the JavaScript Ninja, Second Edition

The chapter introduction recalls a simplified event-loop model and defines timers as asynchronous delays before previewing the chapter’s topics.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction recalls a simplified event-loop model and defines timers as asynchronous delays before previewing the chapter’s topics.

Key terms: single-threaded execution model, event loop, event queue, timers, asynchronously

13.1 Diving into the event loop

This section explains the event loop’s macrotask and microtask queues, how one iteration processes tasks and rendering, and why lengthy tasks can make a page unresponsive.

Key terms: macrotasks, microtasks, promise callbacks, application state, event loop, iteration, macrotask queue, microtask queue, Update rendering, task queues

13.1 Do you know?

This section poses questions to check the reader’s understanding of timer timing, interval callbacks, and event-handler context.

13.1.1 An example with only macrotasks

A two-click example shows how single-threaded JavaScript queues events and runs their handlers in successive event-loop iterations.

Key terms: single-threaded execution model, task queue, click event, event loop, mainline JavaScript code, firstHandler, secondHandler, microtask queue, UI

13.1.2 An example with both macro- and microtasks

This section uses a resolved promise inside a click handler to show how the event loop prioritizes microtasks over queued click tasks and rendering.

Key terms: promise handlers, already resolved promise, microtask, microtask queue, event loop, macrotask, task queue, re-render, promise success microtask

13.2 Taming timers: time-outs and intervals

This section introduces what timers do, the browser methods for creating and clearing them, and why delayed execution can help keep a page responsive.

Key terms: timer, delay, setTimeout, setInterval, clearTimeout, clearInterval, window, host environment, long-running tasks, event loop

13.2.1 Timer execution within the event loop

This section uses a timing example to show why timer callbacks may run later than their stated delays and why intervals behave differently from repeatedly scheduled time-outs.

Key terms: timer task, task queue, setTimeout(fn, delay), event loop, setInterval, interval task, setTimeout, callback, delay

13.2.2 Dealing with computationally expensive processing

The section shows how to keep a browser responsive during expensive JavaScript work by splitting it into timer-scheduled chunks.

Key terms: single-threaded, rendering, unresponsive, timers, chunkSize, iteration, nonblocking operations, time-out delay, callback, microtasks

13.3 Working with events

This section explains how a registered event handler receives event information and distinguishes the event’s target from the element on which the handler was registered.

Key terms: addEventListener, event handler, event object, target, this

13.3.1 Propagating events through the DOM

This section explains how clicks travel through the DOM, how handlers identify the clicked element, and how an ancestor can handle clicks for its descendants.

Key terms: event capturing, event bubbling, capturing phase, bubbling phase, addEventListener, this, event.target, event handler, event target, event delegation

13.3.2 Custom events

The section shows how custom events let shared code signal application-specific conditions without depending on the page code that reacts to them.

Key terms: custom events, loose coupling, close coupling, event handlers, ajax-start, ajax-complete, url property, time-out, pinwheel

13.4 Summary

13.5 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 12: Chapter 12. Working the DOM Chapter 14: Chapter 14. Developing cross-browser strategies →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.