Re: Re: NSTimer: serious drift in Time (in milliseconds)
Re: Re: NSTimer: serious drift in Time (in milliseconds)
- Subject: Re: Re: NSTimer: serious drift in Time (in milliseconds)
- From: "Shawn Erickson" <email@hidden>
- Date: Fri, 10 Nov 2006 15:46:52 -0800
On 11/10/06, Andreas Tell <email@hidden> wrote:
Hi Gilles,
what you like to achieve is absolutely possible using the timer. You
just have to resync it to the realtime clock during each call to the
event handler. This won't remove the jitter, but the drift will
disappear. So in detail what this means is: Let your timer only fire
once, and in the event handler set it up to fire again at a
precalculated absolute point in time 5 seconds ahead. If you start at
10:00:30,000 you would set a new timer to go off at 10:00:35,000 from
within the handler and so on. You do the accumulation of the time
manually.
Consider something like the following (quickly written in mail, yeah I
know it would leak if startTimer is called again)...
In the following count is an integer instance var and startTime is an
NSDate* instance var.
- (void)startTimer:(NSDate*)firstFireTime
{
count = 1;
startTime = [firstFireTime retain];
NSTimer* timer = [[[NSTimer alloc] initWithFireDate:startTime
interval:5.0
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES] autorelease];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)timerFired:(NSTimer*)timer
{
NSDate* now = [NSDate date];
NSLog(@"Timer fired, now = %@, interval since start = %lf (ms)",
now, [startTime timeIntervalSinceDate:now] * 1000.0);
count++;
[timer setFireDate:[[[NSDate alloc] initWithTimeInterval:(5.0 *
count) sinceDate:startTime] autorelease]];
// ... take sample, etc. ...
}
2006-11-10 15:45:45.002 Test[28065] Timer fired, now = 2006-11-10
15:45:45 -0800, interval since start = -285001.006007 (ms)
2006-11-10 15:45:50.002 Test[28065] Timer fired, now = 2006-11-10
15:45:50 -0800, interval since start = -290001.056999 (ms)
2006-11-10 15:45:55.002 Test[28065] Timer fired, now = 2006-11-10
15:45:55 -0800, interval since start = -295001.055002 (ms)
2006-11-10 15:46:00.002 Test[28065] Timer fired, now = 2006-11-10
15:46:00 -0800, interval since start = -300000.863999 (ms)
2006-11-10 15:46:05.002 Test[28065] Timer fired, now = 2006-11-10
15:46:05 -0800, interval since start = -305001.053989 (ms)
2006-11-10 15:46:10.002 Test[28065] Timer fired, now = 2006-11-10
15:46:10 -0800, interval since start = -310000.988007 (ms)
2006-11-10 15:46:15.002 Test[28065] Timer fired, now = 2006-11-10
15:46:15 -0800, interval since start = -315001.174986 (ms)
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden