Angular 4, How to update [(ngModel)] with a delay of 1 seconds

前端 未结 6 1548
攒了一身酷
攒了一身酷 2021-02-05 16:17

Since ngModel is updating instantly how to put a delay.

  

        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 16:27

    Lots of solutions using setTimeout(), but this will cause the function to be called each time the model changes, a simple way to prevent this is to clear the timeout first

    e.g.

    timeOut;
    timeOutDuration = 1000;
    
    update_fields(data) {
      clearTimeout(this.timeOut);
      this.timeOut = setTimeout(() => {
         //do something
      }, this.timeOutDuration);
    }
    

    this will only call the function once after the last update is made and the timeOutDuration has elapsed

提交回复
热议问题