[英]call back function are called before the request is finished
I have a like button similar to the one in twitter, which calls a function which deals with the requests and in the call back function which increases the number of likes and change liked bool to true and the button changed to unliked button, the problem is if I clicked fast in the like button the number of likes increases and decreases more than the one user which is allowed 我有一个类似于twitter中的按钮,它调用一个处理请求的函数,并在回调函数中增加喜欢的数量并将喜欢的bool更改为true并且按钮更改为unliked按钮,问题是如果我在相似按钮中快速点击,则喜欢的数量比允许的一个用户增加或减少更多
I have tried to put delay 1 second inside the call back function and outside it but it didn't have the best effect 我试图将延迟1秒放回到回调函数内部并在其外部,但它没有达到最佳效果
likeDecision(kweek: Kweek): void {
// not in my profile
if (this.callCommonFunc) {
this.kweekFunc.like(kweek);
return;
}
// in my profile
this.kweekService.likeKweek(kweek.id).subscribe(() => {
this.likeCallBack(kweek);
});
}
likeCallBack(kweek: Kweek): void {
this.kweeks.forEach(loopKweek => {
if (loopKweek.id === kweek.id) {
loopKweek.liked_by_user = true;
loopKweek.number_of_likes++;
}
});
}
unlikeDecision(kweek: Kweek): void {
// not in my profile
if (this.callCommonFunc) {
this.kweekFunc.unlike(kweek);
return;
}
// in my profile
this.kweekService.unlikeKweek(kweek.id).subscribe(() => {
this.unlikeCallBack(kweek);
});
}
unlikeCallBack(kweek: Kweek): void {
this.kweeks.forEach(loopKweek => {
if (loopKweek.id === kweek.id) {
loopKweek.liked_by_user = false;
loopKweek.number_of_likes--;
}
});
}
I want to click as fast as I want and the callback function just called when the request finished 我想按照我想要的速度单击,并在请求完成时调用回调函数
this is the http request which I use in like: 这是我使用的http请求:
likeKweek(id: string): Observable<any> {
return this.http.post<any>(`${this.base}kweeks/like`, { id: id }).pipe(
map(res => res),
catchError(this.handleError)
);
}
I think you can use a Boolean to check if you are still executing the last request. 我认为您可以使用布尔值来检查您是否仍在执行上一个请求。 for example you can use the next algo.
例如,你可以使用下一个算法。
like ( ) {
if(!busy) {
busy = true;
requestfunc => {
set likes;
busy = false;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.