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

@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{
     // 方法实现
}

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容