# How can I calculate DeltaTime in seconds?

Here is how I've been trying to implement delta time based on different internet resources.

var startTime:Number = getTimer();
game.Update(deltaTime);
deltaTime = Number(getTimer() - startTime) * 0.001;


My issue with this is it doesn't seem to be giving me accurate timing. The main update shows the frame time at 0.001 and when reinitializing the level it goes to 0.002. I'm using dt else where for a timer and later on time based physics so I would like it to work as expected. I must be missing something silly.

I have tried different methods to get the elapsed time and this one has worked best for me so far:

private var _t:int;

// initialize _t and add ENTER_FRAME listener
private function init():void {
_t = getTimer();

• @user1133079 Glad it helped. The problem with your approach was that you were only measuring the time needed for your update call. You need to measure the time used for a frame and therefore only use getTimer once in the update. If your update takes less time than the current FPS, your delta time will be 1/FPS if your update takes longer (due to heavier calculations) the next frame will be delayed and it will also be correctly reflected in the dt variable. – bummzack Jun 2 '12 at 9:22