typescript
39 lines · 7 steps
Building a trailing-edge throttle in TypeScript
A generic throttle that runs immediately when allowed and otherwise schedules one trailing call, used to drive a scroll progress bar.
Explained by
highlit
1function throttle<T extends (...args: any[]) => void>(
2 fn: T,
3 limit: number
4): (...args: Parameters<T>) => void {
5 let lastRun = 0;
6 let queued: ReturnType<typeof setTimeout> | null = null;
7
8 return function (this: unknown, ...args: Parameters<T>) {
9 const now = Date.now();
10 const remaining = limit - (now - lastRun);
11
12 if (remaining <= 0) {
13 if (queued) {
14 clearTimeout(queued);
15 queued = null;
16 }
17 lastRun = now;
18 fn.apply(this, args);
19 } else if (!queued) {
20 queued = setTimeout(() => {
21 lastRun = Date.now();
22 queued = null;
23 fn.apply(this, args);
24 }, remaining);
25 }
26 };
27}
28
29const progressBar = document.querySelector<HTMLElement>('#reading-progress');
30
31const updateProgress = throttle(() => {
32 if (!progressBar) return;
33 const scrollTop = window.scrollY;
34 const docHeight = document.documentElement.scrollHeight - window.innerHeight;
35 const percent = docHeight > 0 ? (scrollTop / docHeight) * 100 : 0;
36 progressBar.style.width = `${Math.min(percent, 100)}%`;
37}, 100);
38
39window.addEventListener('scroll', updateProgress, { passive: true });
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A throttle bounds how often a function runs by tracking the timestamp of the last execution.
- 2Scheduling a trailing call ensures the final invocation isn't lost when events stop mid-window.
- 3Generic constraints with Parameters<T> let one wrapper preserve any function's signature type-safely.
Related explainers
typescript
import { CallHandler, ExecutionContext, Injectable,
Wrapping responses in a NestJS interceptor
interceptors
rxjs
response-shaping
Intermediate
7 steps
typescript
type RetryOptions = { retries?: number; timeoutMs?: number; baseDelayMs?: number;
Retry with timeout and backoff in TypeScript
promises
retry
exponential-backoff
Intermediate
10 steps
typescript
import { Pipe, PipeTransform, ChangeDetectorRef, NgZone, OnDestroy } from '@angular/core'; @Pipe({ name: 'timeAgo',
A self-refreshing timeAgo pipe in Angular
impure-pipe
change-detection
timers
Advanced
10 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
python
import time from collections import defaultdict from threading import Lock
Sliding-window login rate limiting in Flask
rate-limiting
sliding-window
thread-safety
Intermediate
7 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-trailing-edge-throttle-in-typescript-explained-typescript-a65e/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.