NSTimer : periodic action

Hello,

I want to create a timer which, once initialized, calls a handler within a fixed time interval. I must have missed something in the docs.

I create my NSTimer so:

then I add it to what I suppose to be the main event loop:

and I expect this handler to be fired:

I get this error:
+[NSRunLoop addTimer:forMode:]: unrecognized selector sent to class 0x7fff70b6ba68

what is – or what are – my mistake(s) ?

Thank you!

If you use scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: then you don’t need to add it the run loop, it will fire your selector after the time interval you provide as the first parameter.

Ric

The + sign is a big clue: it means you’re messaging a class, not an instance. Add the timer to:

current application's NSRunLoop's mainLoop()

The handler is now fired at regular intervals.

I have a NSLevelIndicator (bind to property gChronometer) I try to adjust into my handler:

on adjustTime_(aTimer)
    my gChronometer's setIntValue_((gChronometer's intValue() as integer) -1)
end

The NSLevelIndicator refuses to adjust its value. Worse, XCode freezes regularly and I have to force to quit. Could you tell me what’s happening?

Is gChronometer really a property bound to the level indicator’s value or is it an IBOutlet for the level indicator?

Ric

Is is an IBOutlet for the level indicator.

I have no idea – I pasted your code (both for the timer and the adjustTime method) into a new app, and it worked fine for me. Are you sure something isn’t hooked up wrong in IB? Is your adjustTime method being called?

Ric

What are your min and max values?

I have the max value bound to a property, and it works. But if I try to set the value of the indicator, it fails.

If I remove the binding for the max value and set this max value programmatically, I can set the value.

Can anybody explain this?

It sounds like the max value binding isn’t doing what you think it is.

So.

I bind the max value to a property, I bind my min value to a property, gMinTime : 0 and I set the indicator using another property, gCurrentTime. So my handler becomes:

on adjustTime_(aTimer)
    set my gCurrentTime to gCurrentTime -1
end

and everything works perfectly. Maybe it’s not a good thing to mix coding and binding on the same control. :confused:

Thanks! Last question: how do I get rid of the NSTimer when time reaches zero? Is is possible to do this into the handler? I suppose that ASOC would not appreciate this, a method which releases its caller.

That’s what the invalidate method is for.

Perfect! You have saved me hours of sleep (it’s 02:40 AM here.) :rolleyes:

Thank you, Shane and Ric! Time for bed.