简体   繁体   English

在请求完成之前调用回调函数

[英]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.

相关问题 回调函数在完成请求之前执行 - callback function executes before the finished request.on function javascript / jquery:迭代调用函数; 等到上一个通话结束 - javascript/jquery: Iterative called function; wait till the previous call is finished 在运行函数之前如何确保异步请求已完成 - How ensure async request has finished before running a function 为什么从未调用$ .ajax()回调函数? - Why $.ajax() call back function are never called? 使用extend关键字阻止函数在异步调用完成之前返回 - Prevent function to return before async call is finished using the extend keyword 如何在回调函数中的ajax请求之前访问变量状态 - How to access a variable state from before an ajax request in the call back function Angular 控制流:在函数调用完成之前执行的 return 语句 - Angular Control Flow: return statement executed before function call finished 在整个脚本完成解析之前,是否运行了javascript函数调用? - Does a javascript function call run before the entire script is finished parsing? 如何在调用对象之前立即调用函数 - How to call a function right before object is called Javascript-迭代数组并调用Jquery Ajax请求并等待请求完成,然后再移至下一个请求? - Javascript - Iterate an array and call Jquery Ajax requests and wait for a request finished before moving to next request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM