问题描述
我遇到了问题,因为Angular debounce仅适用于管道并且是可观察的,所以我改用lodash debounce。
每当刷新数据网格表时,它将调用onRefreshTable函数并传递具有表过滤器和排序状态的事件对象。
由于refreshTable()将调用需要较长时间的API,因此我希望将API调用减至最少。
有时,使用相同的参数或不同的参数多次调用表刷新。如果参数不同,则上一个表刷新调用将包含传递给API所需的所有参数。
我的尝试在代码中没有错误的地方,但是它仍然在1秒后使用相同的参数多次调用该函数,
import * as _ from lodash;
onRefreshTable(event: ClrDatagridStateInterface<any>) {
_.debounce((event) => {
this.refreshTable(event)
},1000);
}
refreshTable(event) {
// save state to the rxjs store
// call API which is expensive
}
我正在考虑使用distinctUntilChanged,但是在这种情况下似乎不起作用,因为该函数内部的参数是不可观察的。
也许我应该使用_.cloneDeep来比较以前的参数与当前的参数?
解决方法
您可以使用...
if( (list[number] % 2) == 0 )
{
printf( "%s\n","number is even" );
....
,如果您有比较的参考值,则像这样传递distinctUntilChanged(_.isEqual)
。
代码sinppet:
***.next(JSON.parse(JSON.stringify(customerSearchInfo)))