如何使用Redux Saga去抖动,但先获取,而不是等待n毫秒



redux-saga中是否有方法只在例如5秒内获取一次?

我知道有debounce的功能:

yield debounce(5000, 'SAMPLE_ACTION', actionToFetchFirst)

但我想要的是它先提取,而不是等待5秒才能进行初始提取

我想要的是它先提取,而不是等待5秒来进行初始提取

您可以在lodash.debounce选项中指定leading=true

lodash.debounce(func, [wait=0], [options={}])

[options.leading=false](布尔值(:指定在超时的前沿调用。

lodash.debounce(5000, 'SAMPLE_ACTION', { leading: true });

或者只是添加一个条件,比如:

if (input.length > 1) fetchDebounced();
else fetch();

如果我正确理解你要找的是所谓的throttle,这意味着在5秒内,这个动作只会被调用一次,如果是这样,你可以使用lodash-throttle函数https://lodash.com/docs/4.17.15#throttle

最新更新