js.joedoe.dev

js.joedoe.dev

My JavaScript box

I build websites for a living, and some things I've been copying from project to project for years. They're all here now: library-free recipes, tools I open every week and all my JavaScript links, sorted.

Recipe of the day

Debounce

Waits for the calls to stop and does the work once. I use it in every search box that shows results as you type.

What AI tends to get wrong. It tends to hand you a debounce that drops this and the arguments, or creates it inside the input handler, so every keystroke gets a fresh timer and nothing ever waits.

/* Runs fn once the calls stop for `wait` ms: search boxes, resize, autosave. */
export function debounce(fn, wait = 250) {
	let timer;
	return (...args) => {
		clearTimeout(timer);
		timer = setTimeout(() => fn(...args), wait);
	};
}

// const search = debounce((q) => fetchResults(q), 300);
// input.addEventListener("input", (e) => search(e.target.value));

A few recipes

  • fetch with a timeoutfetch never gives up on its own. AbortSignal.timeout gives it a limit and AbortSignal.any joins it with your own cancel button.
  • View TransitionsAnimates any DOM change: the browser snapshots before and after and blends them. Without support or with reduced motion, it just changes.
  • Removing listeners with a signalPass the same signal to several addEventListener calls and one abort() removes them all. No more keeping every handler around.
  • Dates, numbers and lists with Intl“3 days ago”, “1.2K”, “Vue, Svelte and Astro” in the visitor's language, no library. Try the demo in all three languages.

All the recipes →

Looking for CSS or Vue? They live with their sisters, css.joedoe.dev ↗ · vue.joedoe.dev ↗

↑↓ move · ↵ open · esc close