How to toggle on/off timer programmatically - iphone

This is what I want to do:
I want a timer, to fire a method and then, in the end of this method, be toggled off, and turn on an other timer on another method, and then entering a loop.
So what are the codes used to toggle between on and off the timer on a method?
In Delphi I use:
timer.enable:=True; // timer.enable:=False;
Are there a similar way to do it on objective-c?
I'm using Xcode 4.4
Thanks!

To turn the timer off, call invalidate on your timer like so:
[yourTimer invalidate]
And then to start a new one:
NSTimer *newTimer;
newTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 //Every how many seconds
target:self
selector:#selector(methodToCall)
userInfo:nil
repeats:YES];

Assuming your NSTimer is called "timer", you can use...
[timer invalidate]
to stop the timer. To make a timer pass a message to it's target method instantly, use
[timer fire]
To start a timer, you use one of the constructor methods listed in the documentation (https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html) such as
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(doThisWhenTimerFires:) userInfo:nil repeats:NO]
- (void)doThisWhenTimerFires:(NSTimer *)timer
{
//code here
}

Related

NSTimer how to disable while timer is still active?

I have created a timer,
self.scanTimer = [NSTimer scheduledTimerWithTimeInterval:TIMEOUT_SECONDS target:self selector:#selector(meInterrupted:) userInfo:nil repeats:NO];
and some point in time in my code, i feel to disable it before timeout happens, so that the meInterrupted not to be called?. How to achieve this ?
Just call [self.scanTimer invalidate]; This will keep the timer from ever firing if it is called before the timer fires.

How can I stop my NSTimer thread from further executing the selector

Currently I am creating a NSTimer that calls my method every one second.
I do this like so:
[NSTimer scheduledTimerWithInterval:1 target:self selector:#selector(clocktick) userInfo:nil repeats:YES];
How can I stop this from calling clocktick?
I have tried assigning it like so: NSTimer *myTimer = [NSTimer ... and then using myTimer = nil; but that did nothing.
You need [myTimer invalidate] that will kill the timer for you.
You have to hold a reference to it, and then send the invalidate message to it:
[myTimer invalidate];
I hope it helps!
Have you tried using 'invalidate' ([timerInstance invalidate]; for example)?
Or is the problem that you don't have a reference to the timer instance anymore?

NSTimer not running

I've got a program in which I want to code a timer which checks wether the user is idle or not. For that I wrote following code:
if (!idleTimer) {
NSLog(#"make");
idleTimer=[[NSTimer alloc]initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:10.0]interval:10 target:self selector:#selector(idleTimerExceeded) userInfo:nil repeats:NO];
NSLog(#"madetimer with: %f, %#", idleTimer.timeInterval, idleTimer.fireDate);
}else {
NSLog(#"no reset timer: %f", idleTimer.timeInterval);
if (fabs([idleTimer.fireDate timeIntervalSinceNow]) < 9) {
NSLog(#"reset");
[idleTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
}
}
But somehow my logs show that the interval is always 0.0000 . Which means that something is wrong here. Can anybody help me?
ive never seen a timer look so complicated. try this:
write a method that checks if the user is idle (lets say idleChecker)
then make the timer repeatable and calls that method idleChecker
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
remember to declare the idleChecker method in the .h file
take note if u want to stop the timer event then you need to maintain a reference to it
NSTimer aTimer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(idleChecker) userInfo:nil repeats:YES];
then to stop it
[aTimer invalidate];
as for checking if its running i would just stick a nslog message in there stating something like "check for idle user"

is there timer in ios

I want to create a timer for example to count 2 sec and after each second an nslog type for example 1 sec passed
any suggestion to do that
yes there is NSTimer, use it as -
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector() userInfo:nil repeats:NO];
What you're after is NSTimer.
Which, I can't not point out, even a cursory search of the framework docs would have turned up. Laziness IS one of the three Virtues of the Programmer, but c'mon.
You can call your NSTimer like this
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(changeValue) userInfo:nil repeats:YES];
changeValue function can be like
-(void)changeValue{
NSLog("calling function after every two seconds");
}
You might want to use NSTimers timerWithTimeInterval:target:selector:userInfo:repeats:
method. Point that towards some object that implements a selector which prints your log entries.
Yes, ios having timer which is used for periodically call method or button touchup event. we can use it like following:
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(Your_target_Method)
userInfo:nil
repeats:NO];
NSInteger timer=0;
now call method every 1 sec
-(void)Your_target_Method{
timer=timer+1;
NSLog(#"Timer:%ld",timer);
}
Furthermore, visit https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Timers/Timers.html

How to pause and resume NStimer [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I programmatically pause an NSTimer?
I have a question. How can I pause a countdown using a timer? I am developing a game. In the game, I need to go to next view when the timer pauses, and after coming back I want to resume it.
I try this code in the view:
[mytimer pause];
// to resume
[mytimer resume];
I try that code, but I get a warning saying: "NSTimer may not respond to 'pause'"
I build with that warning and when I press the pause button, the app crashes.
NSTimer indeed doesn't have resume and pause methods so you can end up with a crash in runtime after such a warning. Generally you can create 2 kinds of timers (see NSTimer class reference) one that implements only once and the second, that repeats. Example:
This way you create a timer, that will enter callback myMethod each second.
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(myMethod:)
userInfo:nil
repeats:YES];
You probably will choose this one for your purpose where in your class you should maintain some
BOOL pausevariable and in the callback myMethod do the following:
- (void) myMethod:(NSTimer *) aTimer
{
if (!pause) {
// do something
// update your GUI
}
}
where you update pause accordingly somewhere in your code.
To stop the timer (and release it) call
[myTimer invalidate];
good luck
What you want, is what OpenGLES application brings up to you. You should create 2 methods like this:
- (void)startAnimation
{
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:#selector(selector) userInfo:nil repeats:YES];
}
- (void)stopAnimation
{
[animationTimer invalidate];
animationTimer = nil;
}
It's something like this.
Refer to the NSTimer Class Reference, there is no pause method.