redux-saga中throttle的计时器何时启动?
Great question! I remember scratching my head over this exact detail when I first started working with redux-saga's throttle utility. Let’s break it down clearly:
The timer for throttle(delay, actionPattern, saga) starts the moment the matching action (in your case, INPUT_CHANGED) is dispatched—it does NOT wait for the target saga (handleInput) to finish executing.
Here’s a concrete example using your code:
When you write:
throttle(500, 'INPUT_CHANGED', handleInput)
- The first time an
INPUT_CHANGEDaction is dispatched, redux-saga immediately triggershandleInputand starts a 500ms timer. - Any subsequent
INPUT_CHANGEDactions dispatched during those 500ms are ignored completely—they won’t triggerhandleInputat all. - Once the 500ms timer expires, the next
INPUT_CHANGEDaction will triggerhandleInputagain and reset the timer.
This behavior is intentional: throttle’s job is to limit how often a saga responds to high-frequency actions (like input changes, scroll events, etc.). By starting the timer at action dispatch time, it ensures the specified delay is the minimum gap between consecutive saga runs, regardless of how long the saga itself takes to execute.
If it waited for handleInput to finish before starting the timer, you could end up with longer-than-expected gaps if your saga has slow async logic (e.g., API calls), which defeats the purpose of throttling for responsive UIs.
内容的提问来源于stack exchange,提问作者Mayoul

