How can I reset a timer? - cocoa-touch

I have an app, that every 3 seconds, fires an event that goes to my server and grabs information. I would like to reset the timer count down if an event has transpired in between the time the timer last fired and when it fires subsequently.
So essentially, if my other event fires at 2.5 seconds, and the timer is set to fire in .5 seconds, I would like to reset the timer back to 3 seconds. How can I accomplish this?
I declare the timer as:
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
selector: #selector(getUpdates)
userInfo:nil
repeats: YES];
And psuedo:
-(void) anEventHappened
{
// I got something from the server, I don't need to query it. Reset timer here.
}
-(void) getUpdates
{
// I received no reset, I should check for an update.
}

Does this work?
[_timer invalidate];
_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target: self
selector: #selector(getUpdates)
userInfo:nil
repeats: YES];

Related

Xcode - NSTimer firing at random intervals? No consistency

So I am using NSTimer to run a function every minute, it fires at the correct time for the first 3 or so attempts and then it suddenly starts firing every second. I have no idea why this is happening? Would anyone be able to let me know as to why NSTimer is firing inconsistently?
Here is the line where I have declared my timer.
[NSTimer scheduledTimerWithTimeInterval:60.0f target:self selector:#selector(checkForLocation) userInfo:nil repeats:YES];
It is worth noting that I have NSTimer declared inside of viewDidAppear.
Any help is appreciated,
Thank you.
Try to create a instance for NSTimer by declaring a property. Write a function which will initialize the timer and don’t forget to invalidate it before re-intializing it. Call initializeMyTimer in your -viewDidAppear.
-(void) initializeMyTimer
{
if(myTimer)
{
[myTimer invalidate];
myTimer = nil;
}
myTimer = [NSTimer scheduledTimerWithTimeInterval:60.0f
target:self
selector:#selector(checkForLocation)
userInfo:nil
repeats:YES];
}

NSTimer Will Not Invalidate

-(IBAction) loadWeb: (id) sender {
[_webView loadRequest:nsrequest2];
_webView1.hidden = YES;
_webView.hidden = NO;
self.checkForAdd = [NSTimer scheduledTimerWithTimeInterval:0.4
target:self selector:#selector(checkForAddToCart:) userInfo:nil
repeats:YES];
}
-(IBAction)button1:(id)sender {
[self.checkForAdd invalidate];
}
How would I invalidate the timer? I have tried it without self and many other ways, but for some reason, when I press the button the timer does not invalidate.
Check if loadWeb: is called multiple times. If it is, you will have old timers running without having a reference to them so you can't invalidate them. You should have [self.checkForAdd invalidate]; before you create the new timer.
When you do invalidate the timer, if you aren't creating a new one, set self.checkForAdd = nil; to be sure you aren't going to try using the timer again (some actions will throw an exception if you do).
If at any point you do self.checkForAdd = nil; without having invalidated the timer then you won't have a reference to it so you won't be able to invalidate it in the future.
According to your code, loadWeb is being trigger by a button. So you will be creating new timer every time when button will be press. Its better you create timer on some where else, like create in init, or in viewDidLoad method, because if you are creating this here, you have to make sure you are not creating timer again and again. You can do this by doing a if check
if(!self.checkForAdd){
self.checkForAdd = [NSTimer scheduledTimerWithTimeInterval:0.4
target:self selector:#selector(checkForAddToCart:) userInfo:nil
repeats:YES];
}
Right now its creating new timer on button tap, and previous one have no reference.

Fire IBAction automatically

I have a button and want to fire it automatically by itself without touch.
Is it possible?
-(IBAction)xxx:(id)sender
My answer assumes you have the method:
- (IBAction)someAction:(UIButton *)sender {
}
and that you have a reference to the button in an instance variable named someButton.
If you just need to "fire it" now, simply call it:
[self someAction:someButton];
If you need to "fire it" once, but later, you can do:
// call it 5 seconds from now
[self performSelector:#selector(someAction:) withObject:someButton afterDelay:5.0];
If you want to fire it repeatedly, use a timer:
myTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(buttonTimerFired) userInfo:nil repeats:YES];
- (void)buttonTimerFired {
[self someAction:someButton];
}
Action can be called like every regular function - you can do it by running a timer on something else.
You should use NSTimer for doing your work.
[NSTimer scheduledTimerWithTimeInterval: 0.01f target: self selector: #selector(BtoonMethod) userInfo: nil repeats: NO];
-(void)BtoonMethod
{
// write code for call yor button method
}

Creating an iOS Timer

I am trying to create a "stop watch" type functionality. I have one label (to display the elapsed time) and two buttons (start and stop the timer). The start and stop buttons call the startTimer and stopTimer functions respectively. Every second the timer fires and calls the increaseTimerCount function. I also have an ivar timerCount which holds on to the elapsed time in seconds.
- (void)increaseTimerCount
{
timerCountLabel.text = [NSString stringWithFormat:#"%d", timerCount++];
}
- (IBAction)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(increaseTimerCount) userInfo:nil repeats:YES];
}
- (IBAction)stopTimer
{
[timer invalidate];
[timer release];
}
The problem is that there seems to be a delay when the start button is pressed (which I am assuming is due to reinitializing the timer each time startTimer is called). Is there any way to just pause and resume the timer without invalidating it and recreating it? or a better/alternate way of doing this?
Thanks.
A bit dated but if someone is still interested...
don't "stop" the timer, but stop incrementing during pause, e.g.
- (void)increaseTimerCount
{
if (!self.paused){
timerCount++
}
timerCountLabel.text = [NSString stringWithFormat:#"%d", timerCount];
}
You can't pause the timer without using invalidate. What you can do is add
[timer fire];
after you create the timer in startTimer.

Making a Method happen every 60 seconds when the App is running

I'm wanting to make one of my methods to run every 60 seconds when my App is running, how would I do that?
NSTimer
- (void) startTimer
{
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:60
target:self
selector:#selector(timerFired:)
userInfo:nil
repeats:YES];
}
- (void) stopTimer
{
[self.myTimer invalidate];
}
- (void) timerFired:(NSTimer*)theTimer
{
NSLog(#"yay");
}
While the answer is valid, your question is incomplete.
Why do you need to run this method regularly? If it is to poll iCal for tasks every 60 seconds this isn't the best solution. What you need to be doing is observing the notifications that CalCalender store puts outs