设置连续请求一个方法的时间间隔

2021-06-24  本文已影响0人  GuiiLiin

@interface 里面的成员变量

//  防止textField输入频率太快 接口请求太频繁导致程序崩溃 
@property(nonatomic,strong)NSTimer *currentTimer;
@property(nonatomic,strong)NSTimer *lastTimer;
@property(nonatomic,assign)long long previousInputTime;

设置时间间隔

 //  防止textField输入频率太快 接口请求太频繁导致程序崩溃===================================
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
    NSLog(@"self.previousTime = %lld, time = %lld time - self.previousTime = %lld",self.previousInputTime,(long long)time,(long long)time - self.previousInputTime);
    self.currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(customFunction) userInfo:@"test" repeats:NO];
    if (time - self.previousInputTime < 400) {
        [self.lastTimer invalidate];
    }
    self.previousInputTime = (long long)time;
    self.lastTimer = self.currentTimer;
    //   ==============================================================================

方法实现

- (void)customFunction{
     // 方法实现
}
上一篇下一篇

猜你喜欢

热点阅读