objective-c 取消执行的延迟函数
//延迟执行
[self performSelector:@selector(hidenSelf) withObject:nil afterDelay:2];
//取消延迟执行
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(hidenSelf) object:nil];
#import <Foundation/NSObject.h>
#import <Foundation/NSDate.h>
#import <CoreFoundation/CFRunLoop.h>
@class NSTimer, NSPort, NSArray;
FOUNDATION_EXPORT NSString * const NSDefaultRunLoopMode;
FOUNDATION_EXPORT NSString * const NSRunLoopCommonModes NS_AVAILABLE(10_5, 2_0);
@interface NSRunLoop : NSObject {
@private
id _rl;
id _dperf;
id _perft;
id _info;
id _ports;
void *_reserved[6];
}
+ (NSRunLoop *)currentRunLoop;
+ (NSRunLoop *)mainRunLoop NS_AVAILABLE(10_5, 2_0);
- (NSString *)currentMode;
- (CFRunLoopRef)getCFRunLoop;
- (void)addTimer:(NSTimer *)timer forMode:(NSString *)mode;
- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode;
- (void)removePort:(NSPort *)aPort forMode:(NSString *)mode;
- (NSDate *)limitDateForMode:(NSString *)mode;
- (void)acceptInputForMode:(NSString *)mode beforeDate:(NSDate *)limitDate;
@end
@interface NSRunLoop (NSRunLoopConveniences)
- (void)run;
- (void)runUntilDate:(NSDate *)limitDate;
- (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate;
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
- (void)configureAsServer NS_DEPRECATED(10_0, 10_5, 2_0, 2_0);
#endif
@end
@interface NSObject (NSDelayedPerforming)
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument;
#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
#endif
@end
@interface NSRunLoop (NSOrderedPerform)
- (void)performSelector:(SEL)aSelector target:(id)target argument:(id)arg order:(NSUInteger)order modes:(NSArray *)modes;
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(id)arg;
#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED || __IPHONE_2_0 <= __IPHONE_OS_VERSION_MAX_ALLOWED
- (void)cancelPerformSelectorsWithTarget:(id)target;
#endif
@end
- (void)execute
{
if (m_autoPlayTimer) {
[m_autoPlayTimer invalidate];
m_autoPlayTimer = nil;
}
m_autoPlayTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:selfselector:@selector(autoNext) userInfo:nil repeats:NO];
[m_autoPlayTimer retain];
}
咱们的- (void)execute函数会被自动运行很多次,所以每次近来的时候需要将原来的对象给清空,NSTimer是一个自动释放的对象,一般都是在初始化的时候就[m_autoPlayTimer retain]下,存储在内存方便以后操作,invalidate之后就自动被释放了,再补一个m_autoPlayTimer = nil;所以这样内存中始终会存储一个m_autoPlayTimer的指针,
然后暂停自动播放的时候->
- (void)cancelExecute
{
if (m_autoPlayTimer != nil) {
[m_autoPlayTimer invalidate];
m_autoPlayTimer = nil;
}
}
判断当前是否存在m_autoPlayTimer,如果存在则销毁,这样m_autoPlayTimer就不会执行了,也不会出现把最后一次请求给送走的情况。
http://www.cocoachina.com/bbs/read.php?tid-21341-keyword-NStimer.html