【发布时间】:2022-01-18 08:25:08
【问题描述】:
我正在努力找出在 onChange 元素(例如输入)上使用 _.throttle 的最佳方式。显然它与 _.debounce 的工作方式不同。
// This works as expected! Replacing 'debounce' with 'throttle' does not work though.
<Input onChange={_.debounce(onInputChange, 1000)} />
// This does not work. It work as if the wait-time is set to 0
const page = () => {
const [input, setInput] = React.useState('')
const onInputChange = () => {
console.log('Hi!')
}
return(
<Input onChange={_.throttle(onInputChange, 1000}/>
)}
.
文档说明使用 _.throttle 的方法是确保您只调用该函数一次
// WRONG
$(window).on('scroll', function() {
_.debounce(doSomething, 300);
});
// RIGHT
$(window).on('scroll', _.debounce(doSomething, 200));
谢谢!什么都有帮助
【问题讨论】:
标签: javascript reactjs lodash