Nstimer Timerwithtimeinterval: Not Working

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.

With timerWithTimeInterval:invocation:repeats: and timerWithTimeInterval:target:selector:userInfo:repeats:, you have to add the timer to a run loop manually, with code like this:

[[NSRunLoop mainRunLoop] addTimer:repeatingTimer forMode:NSDefaultRunLoopMode];

Other answers on here suggest that you need to call fire yourself. You don't - it will be called as soon as the timer has been put on a run loop.

NSTimer timerWithTimeInterval not calling selector (Swift)

Replying to myself, I have found the problem. In the previous ViewController I am performing a segue after doing a http connection. In order to make it work, I have embedded the performSegue inside the dispatch:

dispatch_async(dispatch_get_main_queue(), {
self.performSegueWithIdentifier("goMySecondController", sender: nil)
})

In that second Controller is where I have the Timer that was not working. However, after this change is working properly.

Thanks for replies!

NSTimer not firing selector when added with scheduledTimerWithTimeInterval

Call this timer in main thread:

dispatch_async(dispatch_get_main_queue(), ^{
m_timer = [NSTimer scheduledTimerWithTimeInterval:timeOutInSeconds
target:self
selector:@selector(activityIndicatorTimer:)
userInfo:nil
repeats:NO];
});

NSTimer is not repeating as expected

Use

- (NSTimer *)scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

and then you won't have to add it to run loop manually.

NSTimer not calling Method

Either use

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

or

//schedules the timer
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];

From the docs Scheduling Timers in Run Loops

  • Use the scheduledTimerWithTimeInterval:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode.

  • Use the timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)



Swift Code

Either

let timer: NSTimer = NSTimer(timeInterval: 1.0, target: self, selector: "fadeManager:", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)

Or

//schedules the timer
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "fadeManager:", userInfo: nil, repeats: true)

NSTimer scheduledTimerWithTimeInterval - not calling funciton

You never actually start the timer because your startTimer() function will always return before reaching the line of code where you create the timer.

In your guard statement you only continue the execution of the function if internalTimer != nil but the only place where you set the timer is after that statement. Thus, your timer is never created and internalTimer will always be nil.

This should fix your problem:

func startTimer() {
guard internalTimer == nil else {
return print("timer already started")
}
internalTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(TimerService.timeEnded), userInfo: nil, repeats: false)
}

NSTimer not firing the selector

You seem to be a bit mixed up with your timer variable.

You initialize a new timer but you aren't actually using it. Do you want to use the timer you initialized or do you want to you ApplicationDelegate.timer?

Here are the two possible solutions.

Option One (assuming that you have a class instance titled ApplicationDelegate and that it has a timer property):

ApplicationDelegate.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:ApplicationDelegate.timer forMode:NSRunLoopCommonModes];

Option Two:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateModel:) userInfo:str repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

NSTimer not firing

From NSTimer documentation for Mac OS X:

You must add the new timer to a run loop, using addTimer:forMode:.
Then, after seconds seconds have elapsed, the timer fires, sending the
message aSelector to target. (If the timer is configured to repeat,
there is no need to subsequently re-add the timer to the run loop.)

Personally, I typically use +[NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:]

Also note that if you call this method on a background thread, or in a block operation that runs in a background queue, the NSTimer gets destroyed when the background thread does. So be sure you are running it on the main thread or in the main queue if appropriate for your situation.

NSTimer does not call selector or repeat in time interval

Check that movePlayerRight: or movePlayerLeft: are called. And that Auto Layout is not using in the view (I think it is the reason). You can disable Auto Layout in the xib/storyboard file in the first tab of Utilities.

iOS NSTimer not calling selector - not firing

You're calling the wrong selector. Your "setCurrentTime" implementation doesn't take any parameter (e.g. to be properly messaged or called, you should use "selector:@selector(setCurrentTime)".

Now, if you look at Apple's documentation for [NSTimer scheduledTimerWitTimeInterval: target: selector: userInfo: repeats:], Apple says your method should have this signature:

- (void)setCurrentTime: (NSTimer *) timer

Which means your function would look something like this:

-(void)setCurrentTime: (NSTimer *) timer
{
NSLog(@"TEST");
dispatch_async(dispatch_get_main_queue(), ^{
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
[currentTime setText:[dateFormatter stringFromDate:currentDate]];
});
}

and be called like this:

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:0.25 
target:self
selector:@selector(setCurrentTime:)
userInfo:nil
repeats:YES];


Related Topics



Leave a reply



Submit