Custom Event Dispatcher Demo - Online Send & Listen
Create and dispatch custom events with detail. Listen on other elements. Understand pub/sub pattern in vanilla JS.
UD5 Toolkit
Explore how to cancel fetch requests, handle timeouts, and manage event listeners with the AbortController API.
Start a slow network request and cancel it before it completes.
Set a timeout using AbortSignal.timeout() β the request auto-cancels when the deadline passes.
Launch 3 concurrent requests sharing one AbortController. Cancel them all at once.
Use { signal: controller.signal } in addEventListener to cleanly remove listeners.
fetch() requests, event listeners, streams, or custom async tasks. It provides an AbortSignal object that you pass to abortable APIs. When you call controller.abort(), all operations linked to that signal are immediately cancelled. This is far cleaner than older patterns like manually tracking and rejecting promises.
Pass the signal property into the fetch options:
const controller = new AbortController();
fetch('https://api.example.com/data', { signal: controller.signal })
.then(res => res.json())
.catch(err => {
if (err.name === 'AbortError') {
console.log('Request was cancelled');
}
});
// Later, cancel the request:
controller.abort();
The fetch promise rejects with an AbortError DOMException when aborted.
AbortSignal.timeout(ms) is a static method (added in 2022) that creates a signal which automatically aborts after a specified number of milliseconds. This eliminates the need to manually create a controller and set up a setTimeout to call .abort(). Example:
// Auto-cancel after 3 seconds
fetch(url, { signal: AbortSignal.timeout(3000) });
Supported in Chrome 103+, Firefox 100+, Safari 16+.
signal to multiple fetch() calls (or other abortable operations). A single controller.abort() will cancel all of them at once. This is incredibly useful for cleanup in SPA frameworks β for example, aborting all in-flight requests when a component unmounts or a user navigates away. See Scene 3 above for a live demo.
signal option to addEventListener:
const controller = new AbortController();
element.addEventListener('click', handleClick, { signal: controller.signal });
// Later β removes ALL listeners linked to this signal:
controller.abort();
This is cleaner than removeEventListener because you don't need to keep a reference to the handler function. One abort() call can remove dozens of listeners at once. See Scene 4 above for a live demo.
DOMException whose name property is 'AbortError'. You should always check for this in your catch blocks to distinguish intentional cancellations from actual network failures:
fetch(url, { signal })
.catch(err => {
if (err.name === 'AbortError') {
// Intentional β do nothing or clean up
} else {
// Real error β show user a message
}
});
AbortError is not a real error β it indicates deliberate cancellation, not a problem.
AbortSignal.timeout().AbortSignal.timeout() static method is supported in Chrome 103+, Firefox 100+, and Safari 16+. The signal option in addEventListener has the same broad support as AbortController itself. For legacy browsers, a polyfill is available. In 2025, you can confidently use AbortController in production without transpilation for 98%+ of users.
signal.aborted (a boolean property) to check whether the signal has already been aborted:
if (controller.signal.aborted) {
console.log('Already aborted, skipping operation.');
} else {
fetch(url, { signal: controller.signal });
}
You can also listen for the abort event: signal.addEventListener('abort', callback).
controller.abort() is called, the signal is permanently aborted and cannot be reset. If you need to start a new batch of abortable operations, you must create a new AbortController instance. This is by design β it prevents accidentally reusing a stale signal. Each logical "group" of cancellable operations should get its own AbortController.
Create and dispatch custom events with detail. Listen on other elements. Understand pub/sub pattern in vanilla JS.
See the current AudioContext state (suspended, running). Resume it with a button. Understand autoplay policies.
See how to implement file upload progress using both fetch and XHR. Realβtime bar and code snippets for your project.
Get the approximate magnetic declination for a given latitude and longitude. Apply correction to your compass.
Enter a URL to see exactly what Open Graph and Twitter card tags will be shown when shared on social media. Fetch from browser.
See how revertβlayer removes styles from the current cascade layer and falls back to the previous one. Fix specificity wars.
Paste a Base64 data URI and download the image directly. Supports JPEG, PNG, GIF, WebP. Safe local processing.
Realβtime frequency spectrum from your microphone. Visual bars or line. Check room resonance.
Remove solid or similar backgrounds from images using color thresholding. No AI, just quick client-side canvas processing. Download as PNG.
Play an audio file or use mic to see realβtime frequency bars. Choose colors. Great for music videos. Canvas.
Paste a string of 0s and 1s and decode it back into ASCII text. Handles spaceβseparated blocks. Local.
Connect a MIDI controller and see every message in a log. Filter by channel and type. Essential for debugging music apps.
Paste HTML code and extract all comments or remove them completely. See a list of comments found. Clean your markup.
Decode FLAC, Opus, or OGG audio files in the browser using compiled WASM libraries. Play instantly without server uploads. Keep audio private.
Decode an image progressively using the ImageDecoder API. See partial results and metadata. Modern alternative to <img>.
Paste binary sequences and convert them back to readable text. Supports space-separated and 8-bit formatted strings. Quick encoding reconversion.
Upload an image and also see a UI mockup under different deficiencies sideβbyβside. Powerful for inclusive design.
Paste XML sitemap content and extract all <loc> URLs into a clean list. Useful for auditing. Local parsing only.
Parse a Snowflake ID (used by Discord, Twitter) into its timestamp, worker, and sequence components. Instant local decoding.
Write or paste an HTML snippet and generate a PDF using the browser's print engine. Customize page size and margins.
Reverse the NATO phonetic alphabet. Paste 'Bravo Romeo Alpha Victor Oscar' and get 'Bravo'. Essential for call center transcripts.
Upload an image and see its 2D FFT magnitude and phase. Learn how image filtering works. Educatinal tool. Canvas.
Add a dramatic fire or flame overlay to any image. Realistic blending. Adjust intensity. Download the hot result.
View recent earthquakes worldwide on a map. Fetches public USGS GeoJSON feed directly from browser. Real-time data.
Upload a WebP image and automatically generate a <picture> tag with JPEG/PNG fallback. Ensure compatibility everywhere.
Connect your MIDI keyboard and see pressed notes visually on a piano roll. Check velocity, channel, and aftertouch. No DAW needed.
Visualize audio in real time from your microphone or uploaded audio file. See the waveform dance. Uses Web Audio API locally.
Paste HTML or URL to extract canonical link tag. Verify self-referencing. Simple SEO audit step. Local only.
Upload an image and see its RGB and luminance histograms. Understand exposure and color distribution. Photographer tool.
Apply vintage and sepia effects to your photos. Adjust grain, vignette, and tone curves. Download filtered image. Local canvas manipulation.