0

html でデバウンスを使用できない状況があります。ng-model-option: {debounce:1000}

しかし、ディレクティブで関数 doCallback への繰り返しコールバックを削除し、1 秒で 1 つの呼び出しのみを行いたい

html:<input ng-change="vm.GetStuff">

角度指令:

class SuperDirective {
    eventTime: any;
    anotherCallWasMade: boolean;

  static $inject = ['$scope', "$timeout"];
  constructor($scope: angular.IScope, $timeout: ng.ITimeoutService) {
            super();
...
      }



   GetStuff(): void {

          //I've tried something like this but it didn't exactly work             
          if (this.eventTime == undefined) {
                this.eventTime = Date.now();
                this.anotherCallWasMade = true;
            } else {
                var currentTime = Date.now().valueOf();
                var eventTime = this.eventTime.valueOf();
                var sec = (Date.now().valueOf() - this.eventTime.valueOf())/1000;
                if (sec <= 1) {
                //If this is the first attempt after original request then we perform a search once more, if this is another request during the same second we dont do anything..
                    if (this.anotherCallWasMade) {
                        this.anotherCallWasMade = false;
                        this.$timeout(() => {
                            this.eventTime = Date.now();
                            this.get_Items();
                        }, 1000);
                    }
                    return;
                }
                this.anotherCallWasMade = true;
                this.eventTime = Date.now();
            }

                this.$timeout(() => {
                    if (this.doCallback != null) {
                        // I want to call this once in 1s instead of multiple times 
                        this.doCallback ();
                    }
                }, 0);

            }
        }

this.doCallback() への不要な呼び出しを削除する簡単な方法があるかもしれません。

4

0 に答える 0