javascript - 在 Axios 请求上使用 Lodash 的 Debounce 会产生意外结果

标签 javascript reactjs ecmascript-6 lodash axios

每次输入文本时,我的应用程序都会更新。然后对 axios 请求使用 debounce,请求会排队,直到计时器用完,然后它们会立即全部发出。我试图将请求限制为每 10 秒一次。我哪里出错了?

如果这很重要的话,我会在 ReactJS 中这样做。

const debouncedSync = _.debounce(() => {this.sync () }, 10000);

sync = (requestBody) => {
    axios.post('http://127.0.0.1:8000/Server/', requestBody);
  };

最佳答案

请检查faxios debounce

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/

相关文章:

javascript - 巴别塔/Mocha : Mocha installed globally but describe() is not defined

Reactjs 和固定数据表 2 : 100% width for table

reactjs - 组件是否有可能在不改变 props 和 states 的情况下重新渲染

javascript - 单击菜单选项后卡住悬停 onclick

javascript - 我在 "TypeError: Cannot read property ' _id' of undefined 上绞尽脑汁好几天了”

javascript - 测试变量是否为数组的最佳方法

javascript - 如何设置这个嵌套对象的状态?

node.js - TypeScript 中 `import from` 和 `import require` 之间的区别

commonjs - 如何通过Webpack和6to5使用带有es6模块的npm包?

javascript - 是否可以自动密封JS对象?