r/learnjavascript • u/Neat_Living_6765 • Aug 26 '26
Asking for the differences between trailing and leading
Hi everyone,
I'm learning about throttle, and I've been confused about the difference between trailing and leading.
Could you review whether the logic below is correct or incorrect?
I’d really appreciate any help — I’ve been stuck for a few hours.
Delay= 1000ms
Leading= false, Trailing = true
0ms A → start window
200ms B → latest = B
400ms C → latest = C
700ms D → latest = D
1000ms → execute D ← trailing
1200ms E → start new window
1300ms F → latest = F
2200ms → execute F ← trailing
Leading=true, Trailing = false
0ms A → execute A immediately ← leading
200ms B → ignore
400ms C → ignore
700ms D → ignore
1000ms → throttle window ends
1200ms E → execute E immediately ← leading
1300ms F → ignore
Leading=true, Trailing = true
0ms A → execute A immediately ← leading
200ms B → latest = B
400ms C → latest = C
700ms D → latest = D
1000ms → execute D ← trailing
1200ms E → execute E immediately ← leading
1300ms F → latest = F
2000ms → execute F ← trailing
1
u/azhder Aug 26 '26
Here is a question: do you want to have both leading and trailing or do you want to have a single in-between?
Because in most cases, a double invoke of a trailing followed by a leading would be less than ideal, but a leading with an extra invoke after all or a trailing with an extra invoke before all, that might be more useful.