How do I make a count Timer in Objective C? [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working on a game, and i want the program to stops the game after 1 second, how do I do that?

Try this:
[self performSelector:#selector(stop) withObject:nil afterDelay:1];

You can do it with an NSTimer:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(stopGame)
userInfo:nil
repeats:NO];

You can do it like this if you want the timer to get fired every 1 second or whatever delay you want on it..
Doing it, declaring a property..
#property (nonatomic, strong) NSTimer *myTimer;
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myTask) userInfo:nil repeats:YES];
or without one..
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myTask) userInfo:nil repeats:YES];
The myTask function is set to get call every 1 second and you can do whatever you want do in there.. or you set repeats:YES to repeats:NO so it will only get fire/call once.

Related

WebService Call after timeInterval

I want to my Webservice invoke once every 6 Hours.I am newer in iOS. Please help any help would be apperciated.I am stuck.
You can user NSTimer and schedule it for 6 hours
NSTimer *timer = nil;
timer = [NSTimer scheduledTimerWithTimeInterval:6*60*60 //6 hour
target:self
selector:#selector(performAction)
userInfo:nil
repeats:YES];
you can call using NSTimer. this is automatically called which time you set.
e.g.
NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:10.0(your time) target:self selector:#selector(someMethod) userInfo:nil repeats:YES];
-(void)someMethod
{
////API called here...
}
In ViewDidLoad
NSTimer *timer = [NSTimer timerWithTimeInterval:360.0 target:self selector:#selector(hideandview) userInfo:nil repeats:YES];
-(void)hideandview
{
////API called here...
}
You should save the last time in prefence (NSUserDefaults) when you make a call.
Whenever the app starts. Start timer and check current time with last saved time and get the difference.

iOS - Do an action every second [duplicate]

This question already has answers here:
Update a label with speed every x seconds
(2 answers)
Closed 9 years ago.
I want to make an action when every second is passed, for example I got a label and I want it every second to do this :
theLabel.text = #"Hey";
So, how I'll do it?
I think I'll need to use the NSTimer right?
Thanks in Advance
Just use a NSTimer:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doSomething:) userInfo:nil repeats: YES];
And to stop it:
[timer invalidate];
Find the documentation here:
NSTimer Doc
A "plan B" solution:
[self performSelector:#selector(foo) withObject:nil afterDelay:{some time interval}];
-(void)foo
{
//do something..
[self performSelector:#selector(foo) withObject:nil afterDelay:{some time interval}];
}
Yes #kevin you are right NSTimer is the best
just initialize your timer on did load
NSTimer *timerCounter = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(doMyWork) userInfo:nil repeats: YES]; //make repeats YES
and do the below
-(void)doMyWork
{
theLabel.text = #"Hey";
// Or what ever you want to do
}

iOS NSTimer not working? [duplicate]

This question already has answers here:
NSTimer timerWithTimeInterval: not working
(4 answers)
Closed 8 years ago.
This is my exact code, and it doesn't seem to be working. Can you tell me what I am doing wrong? Note that refreshTimer was already declared in the private interface.
-(void)viewDidLoad {
refreshTimer = [NSTimer timerWithTimeInterval:1 target:self selector:#selector(timerTest) userInfo:nil repeats:YES];
}
-(void)timerTest {
NSLog(#"Timer Worked");
}
Give scheduledTimerWithTimeInterval a try:
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(myMethod) userInfo:nil repeats:YES];
Quoting: NSTimer timerWithTimeInterval: not working
scheduledTimerWithTimeInterval:invocation:repeats: and scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: create timers that get automatically added to an NSRunLoop, meaning that you don't have to add them yourself. Having them added to an NSRunLoop is what causes them to fire.
There is two-option.
If using a timerWithTimeInterval
use a following like it.
refreshTimer = [NSTimer timerWithTimeInterval:1.0f target:self selector:#selector(timerHandler) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:refreshTimer forMode:NSRunLoopCommonModes];
also mode is two-option. NSDefaultRunLoopMode vs NSRunLoopCommonModes
more Information. refer a this documentation: RunLoopManagement
If using a scheduledTimerWithTimeInterval
use a following like it.
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(timerHandler) userInfo:nil repeats:YES];
scheduled timers are automatically added to the run loop.
more information. refer a this documentation: Timer Programming Topics
In summary
The "timerWithTimeInterval" you have to remember
to add the timer to the run loop that you want to add on.
The "scheduledTimerWithTimeInterval" default auto creates a timer that runs in
the current loop.

IOS: stop a NSTimer [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSTimer doesn't stop
I have this code:
[NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
- (void) targetMethod:(NSTimer) timer{
NSLog(#"Hello World");}
in targetMethod I can stop timer with [timer invalidate], but out of this method, How can I stop targetMethod?
You can keep your NSTimer in a variable and stop the timer using the invalidate method. Like the following:
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
[myTimer invalidate];
One way to do it is to create NSTimer *timer; as a global variable so you can have a handle to the timer. Some thing likes this:
NSTimer *timer; //global var
timer = [NSTimer scheduledTimerWithTimeInterval:110.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:YES];
To stop timer somewhere in the same class:
[timer invalidate];

How to stop NStimer event? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSTimer doesn't stop
In my application I am using NStimer to call an animation function every 3 seconds. I want to stop this timer and called another event while the timer is still running. Is this possible?
#interface
NSTimer *autoTimer;
#implementation
// Start timer
autoTimer = [NSTimer scheduledTimerWithTimeInterval:(3.0)
target:self
selector:#selector(autoTimerFired:)
userInfo:nil
repeats:YES];
// Stop timer:
[autoTimer invalidate];
autoTimer = nil;
First, you want to keep a pointer to the timer
self.packetTimer = [NSTimer timerWithTimeInterval:CONNECTION_TIMEOUT target:self selector:#selector(connectionTimeout:) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:packetTimer forMode:NSDefaultRunLoopMode];
If somewhere else in your code you want to cancel it, just call:
[self.packetTimer invalidate];
self.packetTimer = nil;

Resources