javascript
39 lines · 6 steps
Building a leading-and-trailing throttle
A closure-based throttle that fires immediately, then guarantees a trailing call for events that arrive during the cooldown.
Explained by
highlit
1function throttle(fn, wait) {
2 let lastCall = 0;
3 let timeoutId = null;
4 let lastArgs = null;
5 let lastThis = null;
6
7 function invoke() {
8 lastCall = Date.now();
9 timeoutId = null;
10 fn.apply(lastThis, lastArgs);
11 lastArgs = lastThis = null;
12 }
13
14 function throttled(...args) {
15 const now = Date.now();
16 const remaining = wait - (now - lastCall);
17 lastArgs = args;
18 lastThis = this;
19
20 if (remaining <= 0) {
21 if (timeoutId) {
22 clearTimeout(timeoutId);
23 timeoutId = null;
24 }
25 invoke();
26 } else if (!timeoutId) {
27 timeoutId = setTimeout(invoke, remaining);
28 }
29 }
30
31 throttled.cancel = function () {
32 clearTimeout(timeoutId);
33 timeoutId = null;
34 lastCall = 0;
35 lastArgs = lastThis = null;
36 };
37
38 return throttled;
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Closures let a returned function carry private state like timers and timestamps across calls.
- 2Capturing both args and this keeps a deferred trailing invocation faithful to the original call site.
- 3Exposing a cancel method gives callers a way to clear pending timers and avoid stale invocations.
Related explainers
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
python
import secrets from django.contrib.auth import authenticate, login from django.core.cache import cache
Two-factor login with OTP in Django
two-factor-auth
one-time-passwords
caching
Intermediate
9 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 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-leading-and-trailing-throttle-explained-javascript-da62/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.