Ember.run.debounce()

debounce (target, method, args*, wait, immediate) Arraypublic

Defined in packages/ember-metal/lib/run_loop.js:552

Delay calling the target method until the debounce period has elapsed with no additional debounce calls. If debounce is called again before the specified time has elapsed, the timer is reset and the entire period must pass again before the target method is called.

This method should be used when an event may be called multiple times but the action should only be called once when the event is done firing. A common example is for scroll events where you only want updates to happen once scrolling has ceased.

function whoRan() {
  console.log(this.name + ' ran.');
}

let myContext = { name: 'debounce' };

run.debounce(myContext, whoRan, 150);

// less than 150ms passes
run.debounce(myContext, whoRan, 150);

// 150ms passes
// whoRan is invoked with context myContext
// console logs 'debounce ran.' one time.

Immediate allows you to run the function immediately, but debounce other calls for this function until the wait time has elapsed. If debounce is called again before the specified time has elapsed, the timer is reset and the entire period must pass again before the method can be called again.

function whoRan() {
  console.log(this.name + ' ran.');
}

let myContext = { name: 'debounce' };

run.debounce(myContext, whoRan, 150, true);

// console logs 'debounce ran.' one time immediately.
// 100ms passes
run.debounce(myContext, whoRan, 150, true);

// 150ms passes and nothing else is logged to the console and
// the debouncee is no longer being watched
run.debounce(myContext, whoRan, 150, true);

// console logs 'debounce ran.' one time immediately.
// 150ms passes and nothing else is logged to the console and
// the debouncee is no longer being watched

Parameters:

target [Object]
target of method to invoke
method Function|String
The method to invoke. May be a function or a string. If you pass a string then it will be looked up on the passed target.
args* [Object]
Optional arguments to pass to the timeout.
wait Number
Number of milliseconds to wait.
immediate Boolean
Trigger the function on the leading instead of the trailing edge of the wait interval. Defaults to false.

Returns:

Array
Timer information for use in cancelling, see `run.cancel`.
doc_EmberJs
2016-11-30 16:51:39
Comments
Leave a Comment

Please login to continue.