每次输入文本时,我的应用程序都会更新。然后对 axios 请求使用 debounce,请求会排队,直到计时器用完,然后它们会立即全部发出。我试图将请求限制为每 10 秒一次。我哪里出错了?
如果这很重要的话,我会在 ReactJS 中这样做。
const debouncedSync = _.debounce(() => {this.sync () }, 10000);
sync = (requestBody) => {
axios.post('http://127.0.0.1:8000/Server/', requestBody);
};
最佳答案
let fetcher = faxios()
.baseURL('http://jsonplaceholder.typicode.com')
.url('posts', 1, 'comments')
.debounce(1 * 1000); // debounce time 1000ms
fetcher
.FETCH // => Promise
.then(res => console.log('res1:',res))
.catch(err => console.log('error1:', err));
fetcher
.FETCH // => Promise
.then(res => console.log('res2:', res))
.catch(err => console.log('error2:', err));
fetcher
.FETCH // => Promise
.then(res => console.log('res3:',res))
.catch(err => console.log('error3:', err));
fetcher
.FETCH // => Promise
.then(res => console.log('res4:', res))
.catch(err => console.log('error4:', err));
/**
error1:debounced
error2:debounced
error3:debounced
res4:{...}
**/
关于javascript - 在 Axios 请求上使用 Lodash 的 Debounce 会产生意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39809844/