Skip to content

Instantly share code, notes, and snippets.

@marksands
Forked from berzniz/NSObject+Debounce.h
Created February 1, 2017 19:20
Show Gist options
  • Save marksands/0aa06ccde8bc5a4633e4f49fddcb73d9 to your computer and use it in GitHub Desktop.
Save marksands/0aa06ccde8bc5a4633e4f49fddcb73d9 to your computer and use it in GitHub Desktop.
Debounce method for Objective C
@interface NSObject (Debounce)
- (void)debounce:(SEL)action delay:(NSTimeInterval)delay;
@end
@implementation NSObject (Debounce)
- (void)debounce:(SEL)action delay:(NSTimeInterval)delay
{
__weak typeof(self) weakSelf = self;
[NSObject cancelPreviousPerformRequestsWithTarget:weakSelf selector:action object:nil];
[weakSelf performSelector:action withObject:nil afterDelay:delay];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment