I have to delay 2 seconds using NSTimer. How to do it?

I want to create a 2 second delay using NSTimer, how to initialize a timer in a program?

+3
source share
4 answers

A few options here.

If you just need a 2 second delay, you can use the sleep () function

#include<unistd.h>
...
sleep(2);

Or you can use NSTimer so

[NSTimer    scheduledTimerWithTimeInterval:2.0    target:self    selector:@selector(fireMethod)    userInfo:nil repeats:NO];

And in your class, you will have a method defined as

-(void)fireMethod
{
//Do stuff 
}
+12
source

Here you go ...

[NSTimer scheduledTimerWithTimeInterval:2 
                                     target:self 
                                   selector:@selector(action) 
                                   userInfo:nil 
                                    repeats:NO];
+4
source

: [NSThread sleepForTimeInterval: 10.0];

+1

: /. , , , , . :

// code that will block the UI when done in the main thread
- (void) methodC {
  doA();
  delay(2);
  doB();
}

, :

- (void) methodA {
  doA();
  return;  // back to the run loop where other useful stuff might happen
}

- (void) methodB {
  doB();
}

and you can schedule method B using NSTimer at the end of method A, NSTimer launched by any methodA method or, best of all, using an asynchronous procedure to complete something started by methodA.

-1
source

Source: https://habr.com/ru/post/1764746/


All Articles