Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@berzniz
Created January 25, 2014 16:18
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save berzniz/8618806 to your computer and use it in GitHub Desktop.
Save berzniz/8618806 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
@AntiMoron
Copy link

Awesome. Much shorter and better than my colleague's implementation. I've introduced this to him.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment