如何在异步功能上使用反跳功能? [英] How to use debounce on async function?
本文介绍了如何在异步功能上使用反跳功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 async
函数上使用去抖动
?我的 vue
-app中有一个方法,该方法可从API中获取数据,该方法连续调用该API,而我想避免这种情况。
How can I use debounce
on an async
function? I have a method within my vue
-app which reveives data from an API which calls the API continuosly which I want to avoid.
这是我的方法:
methods: {
async getAlbums () {
const response = await AlbumService.fetchAlbums()
this.albums = response.data.albums
}
}
我以前安装了 lodash
,所以我该怎么实现呢?
I've installed lodash
previously so how can I achieve that?
推荐答案
Lodash的 去抖动
函数接受一个函数,等待时间并返回一个函数。
Lodash's debounce
function takes in a function , time to wait and returns a function.
所以要这样做:
methods: {
getAlbums: _.debounce(async function() {
const response = await AlbumService.fetchAlbums();
this.albums = response.data.albums;
}, 1000);
}
这篇关于如何在异步功能上使用反跳功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文