How to write a debounce service in AngularJS

前端 未结 7 704
说谎
说谎 2020-11-27 11:01

The underscore library provides a debounce function that prevents multiple calls to a function within a set period of time. Their version makes use of setTimeout.

H

7条回答
  •  遥遥无期
    2020-11-27 11:39

    Angular 1.3 has debounce as standard

    Worth mentioning that debounce comes built in with Angular 1.3. As you'd expect, it's implemented as a directive. You can do this:

    
    

    The $scope.address attribute is not updated until 500ms after the last keystroke.

    If you need more control

    If you want more granularity, you can set different bounce times for different events:

    
    

    Here for example we have a 500ms debounce for a keystroke, and no debounce for a blur.

    Documentation

    Read the documentation here: https://docs.angularjs.org/api/ng/directive/ngModelOptions

提交回复
热议问题