ebs.debounce
Delays the execution of a function for a preset time after it was invoked. If the function is triggered again during the waiting period, the delay is reset.
HINT
ebs.debounce allows you to prevent function calls until the call rate for that function falls below a specific threshold. For instance, a function that displays search suggestions can be triggered at a specific time after the user has stopped typing, instead of being triggered repeatedly on every key press.Syntax
Copy
function ebs.debounce(func: Function, wait: number, immediate: boolean): () => any;
| Parameter | Type | Description |
|---|---|---|
func
|
Function | Function to be debounced. |
wait
|
number | Delay period in milliseconds. |
immediate
|
boolean | If true, triggers the function immediately, then applies the delay(s) when the function calls are not allowed. |
Return Value
Returns a function (closure) with the bound variables in the debounce scope.
Examples
In this example:
- We define the myFunction function that displays the hello message on screen, then waits for 3 seconds (3000 milliseconds) from the last call before the message can be displayed again (the immediate parameter is set to true). For details on how to display messages on screen, see ebs.showMessage.
- We allow the user to trigger the function by clicking the sayHello button.
Copy
var myFunction = ebs.debounce(function(){ebs.showMessage("hello")}, 3000, true);
/* Click event for the sayHello button */
$('#sayHello').on('click', function (event)
{
myFunction();
});