Created
          February 18, 2025 04:26 
        
      - 
      
- 
        Save catwhocode/0fbd3fe2ade2dcb05156d4665e5f7db7 to your computer and use it in GitHub Desktop. 
    Debounce Decorator
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment