Ionic debounce problem

I wish to use ionic.debounce but I do not really understand the behavior.

If I read the doc:

/**
     * Only call a function once in the given interval.
     *
     * @param func {Function} the function to call
     * @param wait {int} how long to wait before/after to allow function calls
     * @param immediate {boolean} whether to call immediately or after the wait interval
     */

I understand that if I call “myFunctionToCall” (see below) 10 times per second the function will be executed only once.

var myFunctionToCall = ionic.debounce(function(){}, 1000);

I does not work that way apparently, check this out: http://codepen.io/shprink/pen/yzrsD

The function description is misleading. It actually describes a throttling function, not a debounce function. From a comment by Nick Williams on an article at JavaScript Debounce Function

A debounced function is called only once in a given period, delay milliseconds after its last invocation (the timer is reset on every call). Whereas a throttled function is limited to be called no more than once every delay milliseconds.

So you have to stop calling the debounced function for the defined “wait” period for the function to fire again. If you change your example to pass true for the third argument (immediate), the function will fire once and then not again because you keep calling it every half second, so there is no clear second.

I see, thanks for your answer, it makes sense to me now :smile:

Cheers,
Julien