(Re)scheduleTimerWithInterval possible? - ios

I'm using scheduleTimerWithTimeInterval: to perform an animation every N seconds.
In some circumstances I want to reschedule thing so the N starts recounting again i.e.
schduleTimerWithTimeInterval: 5
after 5 seconds perform action
after 5 seconds perform action
2 seconds elapse since the last action then something X happens
5 seconds since X, perform action (i.e. not 5 seconds since the last perform action)
after 5 seconds perform action
after 5 seconds perform action
So when X occurs, is it possible to reset the scheduleTimerWithTimeInterval?

So something like this:
self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(takeAShot) userInfo:nil repeats:YES];
and then when x occurs:
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:#selector(takeAShot) userInfo:nil repeats:YES];
Or any number of ways with repeating and non repeating timers

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.

ns timer duplicate created 2-3 times

I am working on a app in which i have created a timer which called method make sound every 1 sec..
after 10 sec it is invalidated automatically but on one button click event i have stopped timer before 10 sec complete,but when again i come to that view it creates two timer and the call method twice and for third it called same method 3 times
DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(MakeSound) userInfo:nil repeats:YES];
and i have invalidate that like this on button click event
[DecilneTimer invalidate];
DecilneTimer=nil;
how to solve duplicate creation of timer???
You should use:
self.DecilneTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(MakeSound) userInfo:nil repeats:YES];
Using self.propertyName = ... you are using the property accessor and retaining the NSTimer automatically so that it doesnt get created again and again. And what you were doing was simply changing the ivar value directly

iOS Timer loop that executes a certain action every X minutes

I am trying to execute a certain block of code every x amount of time, but it seems that all I am doing is executing it during that time. Here's a block of my code.
while (TRUE) {
NSTimer *countDown = [NSTimer
scheduledTimerWithTimeInterval:(x)
target:self
selector:#selector(timerHandle)
userInfo:nil
repeats:YES];
}
Any ideas as to how to do it?
As written, this is an infinite loop, creating an NSTimer every loop iteration.
Try it without the while loop. This should cause [self timerHandle] to be invoked on interval x by a single background thread/timer. The Apple guide to NSTimer usage (including as others point out, how to properly stop your timed task) is here.
Try this: (It will call executeMethod on every 5 sec)
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(executeMethod)
userInfo:nil
repeats:YES];
});
}
else{
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(executeMethod)
userInfo:nil
repeats:YES];
}
Write the code you want to be executed in executeMethod method. Hope this helps.. :)

Periodically call to a web service in ios

I have a NSTimer like this:
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(sliderUpdate:) userInfo:nil repeats:YES];
-(void)sliderUpdate:(id)sender
{
int currentTime = (int)((newPlayer.currentTime.value)/newPlayer.currentTime.timescale);
slider.value=currentTime;
NSLog(#"%i",currentTime);
song.currentTime=currentTime;
int currentPoint=(int)((newPlayer.currentTime.value)/newPlayer.currentTime.timescale);
int pointMins=(int)(currentPoint/60);
int pointSec=(int)(currentPoint%60);
NSString *strMinlabel=[NSString stringWithFormat:#"%02d:%02d",pointMins,pointSec];
lblSlidermin.text=strMinlabel;
song.strslidermin=strMinlabel;
}
What I want to do, is, in each and every 45 seconds, call a web service:
Example: 1st call in 45 seconds, second in 90 seconds, etc.
How can I do this in this NSTimer?
You just have to modify your NSTimer for 45 seconds:
[NSTimer scheduledTimerWithTimeInterval:45 target:self selector:#selector(sliderUpdate:) userInfo:nil repeats:YES];

while loop using NSTimer

I want to create a NSTimer that runs for lets say 10 minutes. I then want to write a while loop aftewards delaying 10 minutes of time before the line afterwards is executed. For example.
NSTimer * countDown = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector userInfo:nil repeats:NO];
while (countDown == stil running (hasnt reached 10 minute mark) ){
// kill 10 minutes of time
}//when 10 minutes is up
execute next line of code
First
The timeInterval parameter of scheduledTimerWithTimeInterval: method is in seconds. If you want in minutes, don't forget to multiply by 60.
Second
Why would you want to wait the countDown with a while like that? Just call the lines you want to execute 10 minutes later in the selector that NSTimer fires.
NSTimer * countDown = [NSTimer
scheduledTimerWithTimeInterval:(10.0 * 60)
target:self
selector:#selector(tenMinutesLater)
userInfo:nil
repeats:NO];
And then
-(void)tenMinutesLater
{
//put some code here. This will be executed 10 minutes later the NSTimer was initialized
}
Instead of a while loop, just make a new timer inside of a method called by your first timer.
NSTimer *countDown = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(startSecondTimer) userInfo:nil repeats:NO];
- (void)startSecondTimer
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:600 target:self selector:#selector(doSomething) userInfo:nil repeats:NO];
}
PS, the time interval is in seconds - 10 mins = 600 seconds, not 10.

Resources