美文网首页
设置连续请求一个方法的时间间隔

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

作者: GuiiLiin | 来源:发表于2021-06-24 09:31 被阅读0次

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

    相关文章

      网友评论

          本文标题:设置连续请求一个方法的时间间隔

          本文链接:https://www.haomeiwen.com/subject/hulwyltx.html