How to stop/invalidate NStimer - iphone

I am using
[NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
I want to stop calling this timer one.
viewDidDisappear
How can i do that? invalidate?

Declare NSTimer *myTimer in .h file.
Assign instance as tom said like this
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
Stop and Invalidate using this
- (void) viewDidDisappear:(BOOL)animated
{
[myTimer invalidate];
myTimer = nil;
}

Try this
viewController .h
NSTimer *timer;
viewcontroller.m
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(pollTime)
userInfo:nil
repeats:YES];
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[timer invalidate];
}

Yes, you would use the - (void)invalidate instance method of NSTimer.
Of course, to do that, you would have to save the NSTimer instance returned from [NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:] into an ivar or property of your view controller, so you can access it in viewDidDisappear.

invalidate Method of NSTimer is use for stop timer
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
If you are not ARC then don't forget [self.timer release];

For stopping or invalidating NSTimer first you have to create instance for NSTimer in Globally.
Timer=[NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:#selector(update)
userInfo:nil
repeats:YES];
after that try like this,
if (Timer != nil) {
[Timer invalidate];
Timer = nil;
}

Related

How to run another thread and using that thread run timer to repeat process?

Hi i want to start a timer with another thread and want to repeat process in that thread. i had tried but it doesn't work.
my code snippet is given below
[NSThread detachNewThreadSelector:#selector(setupTimerThread) toTarget:self withObject:nil];
-(void)setupTimerThread
{
NSLog(#"inside setup timer thread");
// self.startTiemr = [[NSTimer alloc]init];
startTiemr = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:#selector(triggerTimer) userInfo:nil repeats:YES];
runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:startTiemr forMode:NSRunLoopCommonModes];
[runLoop run];
}
can anyone suggest me a proper way to do that?
Thanks in advance.
#import "TimePassViewController.h"
#interface TimePassViewController ()
#end
#implementation TimePassViewController
{
NSTimer *timer;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// [self performSelectorOnMainThread:#selector(yourmethod) withObject:nil waitUntilDone:NO];
NSThread* myThread = [[NSThread alloc] initWithTarget:self
selector:#selector(yourmethod)
object:nil];
[myThread start]; // Actually create the thread
}
-(void)yourmethod
{
#autoreleasepool
{
NSRunLoop *TimerRunLoop = [NSRunLoop currentRunLoop];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timerMethod) userInfo:nil repeats:YES];
[TimerRunLoop run];
}
//timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timerMethod) userInfo:nil repeats:YES];
}
-(void)timerMethod
{
NSLog(#"aaaaaaaaaaaa");
//[timer invalidate];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
try this code:
NStimer *uptimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(bachground_todaylist) userInfo:nil repeats:YES];
-(void)bachground_todaylist
{
[self performSelectorInBackground:#selector(yourmethod) withObject:nil];
}
you wants to stop proces
[uptimer invalidate];

Stop NSTimer running from application didFinishLaunchingWithOptions

I have start a timer to call a method after 10 seconds. the timer start from didFinishLaunchingWithOptions. it works fine but I want to stop that timer from another UISubclass at the time of logout. How I can do this my code is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[NSThread sleepForTimeInterval:2.0];
[self.window addSubview:viewController.view];
[self setupTimer];
[self.window makeKeyAndVisible];
return YES;
}
-(void)setupTimer;
{
timer = [NSTimer timerWithTimeInterval:10
target:self
selector:#selector(triggerTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)triggerTimer:(NSTimer*)timer;
{
NSLog(#"===============this method is call after every 10 second===========================");
Getlocation *object=[[Getlocation alloc] init];
[object updatelocation];
}
-(void)stopTimer:(NSTimer*)timer;
{
[timer invalidate];
timer=nil;
}
if([timer isValid])
[timer invalidate];
timer = nil;
Take an object of NSTimer.
Write this line where do you want to call method
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(NEWTIMER) userInfo:nil repeats:YES];
Write this code there do you want stop timer.
[timer invalidate];
timer = nil;
[timer release];
if you want stop timer when your app came background .you should write this code into this method.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[timer invalidate];
timer = nil;
[timer release];
}
Write code in logout ButtonAction method.
-(IBAction)LogOutButtonPressed:(id)sender {
[timer invalidate]; timer = nil; [timer release];
}

NSTimer: How to change the scheduled method without having delay?

In my iPhone TIMER app,
after some interval of time I need to change the scheduled method t....
-(void)startTimerAction
{
NSLog(#"Start timer Action");
NSLog(#"Time is Over %#",isTimeOver?#"YES":#"NO");
if(!isTimeOver)
{
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:#selector(ShowActicity) userInfo:nil repeats:YES];
}
else if(isTimeOver)
{
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:#selector(continueClock) userInfo:nil repeats:YES];
}
}
So, to stop and reschedule it I am doing this...in some method..
isTimeOver=YES;
[timer_main invalidate];
timer_main=nil;
[self startTimerAction];
for doing this there is some time delay or unwanted interval to reschedule the timer in a new method....
How can I do it smoothly.....
I just want to reschedule the timer with another method without having delay.....
Right Now I am doing this to fix it.....
NSTimeInterval x=[[timer_main fireDate] timeIntervalSinceNow];
[timer_main invalidate];
timer_main=nil;
sleep(x);
totalCount++;
seconds++;
if(seconds>59)
{
minutes++;
seconds=0;
}
[self startTimerAction];
Thanks...
You can schedule repeating timers with a firedate:
NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate
interval:interval
target:self
selector:#selector(foo)
userInfo:nil
repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

Unable to get NSTimer to work

Using NSTimer for my application in which I have to show countdown timer with initial timing equal to 100sec. It shows 99,then 98..96..94..92...86 and so on. But I want each sec to show up. Here is my code....
-(void)updateTimerLabel{
if (timeRemaining>0 ) {
timeRemaining=timeRemaining-1.0;
timerLabel.text=[NSString stringWithFormat:#"%.0f", timeRemaining];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTimerLabel) userInfo:nil repeats:YES];
}
}
Try following code
int i =100;
-(void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(theActionMethod) userInfo:nil repeats:YES];
}
-(void)theActionMethod
{
if(i > 0)
{
NSString *str = [NSString stringWithFormat:#"%d",i];
lbl.text = str;
}
i--;
}
You are actually re-creating the timer every time your method gets called.
You should try leaving that outside and retaining it until you're finished with it.
#interface SomeClass
NSTimer * aTimer;
#end
#implementation
- (void)createTimer {
aTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTimerLabel) userInfo:nil repeats:YES] retain];
}
- (void)updateTimerLabel {
// do timer updates here
// call [aTimer release]
// and [aTimer invalidate]
// aTimer = nil;
// when you're done
}
#end
you have call updateTimerLabel outside the method.
ex:
-(void)viewDidLoad{timeRemaining=100.0;[self updateTimerLabel];countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTimerLabel) userInfo:nil repeats:YES];}
-(void)updateTimerLabel
{if (timeRemaining>0 )
{timeRemaining=timeRemaining-1.0;NSLog(#"%#",[NSString stringWithFormat:#"%.0f",timeRemaining]);}}

Change the time interval of a Timer

here is my question:
Is it possible to increase the scheduledTimerWithTimeInterval:2 for example of "3" after 10 seconds in ViewDidLoad for example.
E.g., from this:
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
to this:
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:#selector(createNewImage) userInfo:nil repeats:YES];
thank you
sorry for my english I french :/
Reschedule the timer recursively like this:
float gap = 0.50;
[NSTimer scheduledTimerWithTimeInterval:gap target:self selector:#selector(onTimer) userInfo:nil repeats:NO];
-(void) onTimer {
gap = gap + .05;
[NSTimer scheduledTimerWithTimeInterval:gap target:self selector:#selector(onTimer) userInfo:nil repeats:NO];
}
========
Or according to How can I update my NSTimer as I change the value of the time interval
Invalidate it with:
[myTimer invalidate];
Then create a new one with the new time. You may have to set it to nil first as well.
myTimer = nil;
myTimer = [NSTimer scheduledTimerWithTimeInterval:mySlider.value
target:self
selector:#selector(myMethod)
userInfo:nil
repeats:YES];
Use setFireDate: to reschedule the timer. You'll need to keep track of the timer in an ivar. For example:
#property (nonatomic, readwrite, retain) NSTimer *timer;
#synthesize timer=timer_;
- (void)setTimer:(NSTimer *)aTimer {
if (timer_ != aTimer) {
[aTimer retain];
[timer_ invalidate];
[timer_ release];
timer_ = aTimer;
}
- (void)dealloc {
[timer_ invalidate];
[timer_ release];
}
...
self.timer = [NSTimer scheduledTimerWithTimeInterval:...];
...
self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:3]; // reschedule for 3 seconds from now