使用 lodash 的debounce(),我等待 10 秒,然后在我的应用程序状态中设置搜索词。但我想searching在去抖之前设置应用程序的状态:
onChangeText(text) {
setSearching(true);
setSearchTerm(text);
}
render(){
return(
<TextInput style={s.input}
onChangeText={_.debounce(this.onChangeText, 10000, {'leading':true} )}
/>
)
}
Run Code Online (Sandbox Code Playgroud)
从文档来看,这应该在超时的前沿运行,直到事件停止指定的等待时间。实际行为就像根本没有去抖一样,事件每次在调用时都会运行,没有 10 秒的缓冲区。有任何想法吗?删除{'leading':true}确实会适当地进行去抖,但我需要在 10 秒之前在我的应用程序中设置状态。
我正在尝试在进行 api 调用之前向我的应用程序添加去抖动。但是,当我引入 debouce 时,似乎忽略了我的 await 并且由于缺少值而调用函数
export default class App extends Component {
state = {
search: "Cats",
results: []
};
async search(text) {
const giphy = {
baseURL: "https://api.giphy.com/v1/gifs/search",
apiKey: "0UTRbFtkMxAplrohufYco5IY74U8hOes",
tag: text
};
let giphyURL = encodeURI(
giphy.baseURL + "?api_key=" + giphy.apiKey + "&q=" + giphy.tag
);
let data = await fetch(giphyURL);
return data.json();
}
async componentDidMount() {
// get default search
this.onSearch(this.state.text);
}
setSearch = e => {
this.setState({ search: e.target.value });
debounce(() => this.onSearch(this.state.search), 100); …Run Code Online (Sandbox Code Playgroud) 我有一个函数需要编写异步,但我不能以正确的方式完成。希望各位大侠帮忙。
async search (loading, search, vm) {
let vm = this
_.debounce(() => {
let ApiURL = '/users/'
}
let { res } = await api.get(ApiURL) //Error
vm.options = res.data
}, 800)
Run Code Online (Sandbox Code Playgroud) javascript ×2
lodash ×2
debounce ×1
debouncing ×1
react-native ×1
reactjs ×1
vue.js ×1
vuejs2 ×1