DispatcherTimerExtensions.Debounce Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Used to debounce (rate-limit) an event. The action will be postponed and executed after the interval has elapsed. At the end of the interval, the function will be called with the arguments that were passed most recently to the debounced function.
Use this method to control the timer instead of calling Start/Interval/Stop manually.
A scheduled debounce can still be stopped by calling the stop method on the timer instance.
Each timer can only have one debounced function limited at a time.
public static void Debounce (this Windows.UI.Xaml.DispatcherTimer timer, Action action, TimeSpan interval, bool immediate = false);
static member Debounce : Windows.UI.Xaml.DispatcherTimer * Action * TimeSpan * bool -> unit
<Extension()>
Public Sub Debounce (timer As DispatcherTimer, action As Action, interval As TimeSpan, Optional immediate As Boolean = false)
Parameters
- timer
- Windows.UI.Xaml.DispatcherTimer
Timer instance, only one debounced function can be used per timer.
- action
- Action
Action to execute at the end of the interval.
- interval
- TimeSpan
Interval to wait before executing the action.
- immediate
- Boolean
Determines if the action execute on the leading edge instead of trailing edge.
Examples
private DispatcherTimer _typeTimer = new DispatcherTimer();
_typeTimer.Debounce(async () =>
{
// Only executes this code after 0.3 seconds have elapsed since last trigger.
}, TimeSpan.FromSeconds(0.3));