Skip to content

Instantly share code, notes, and snippets.

@catwhocode
Created February 18, 2025 04:26
Show Gist options
  • Select an option

  • Save catwhocode/0fbd3fe2ade2dcb05156d4665e5f7db7 to your computer and use it in GitHub Desktop.

Select an option

Save catwhocode/0fbd3fe2ade2dcb05156d4665e5f7db7 to your computer and use it in GitHub Desktop.

Revisions

  1. catwhocode created this gist Feb 18, 2025.
    17 changes: 17 additions & 0 deletions debounce_decorator.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    import functools
    from threading import Timer

    # source:
    # https://stackoverflow.com/questions/61476962/python-decorator-for-debouncing-including-function-arguments

    def debounce(timeout: float):
    def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
    wrapper.func.cancel()
    wrapper.func = Timer(timeout, func, args, kwargs)
    wrapper.func.start()

    wrapper.func = Timer(timeout, lambda: None)
    return wrapper
    return decorator