CustomTimer does not seem to have the correct interval

I am attempting to resolve this “issue” I have where when I set the interval of my CustomTimer to 500 (500 ms) it does not seem to take effect as expected.

The code looks like this:

class CustomTimer
    public var timeStarted:Float;
    public var interval:Float;
    public var currentTime:Float;
    private var _seconds:Int;
    public var tick:Signal1<Int>;
    public function new(interval:Float)
        timeStarted   = 0;
        currentTime   = 0;
        this.interval = interval;
        tick = new Signal1<Int>();
    public function update(deltaTime:Float)
        currentTime += deltaTime;
        if (currentTime % interval >= 1)
            currentTime = 0;

So every time the currentTime reaches interval, it will reset and the tick event will dispatch. But after building, it still appears as if the animation is happening every 50ms. I’m not sure if it’s because Lib.getTimer() uses microseconds or milliseconds (I would have thought ms) but maybe someone can correct me on that.

If you wish to replicate the results I’m getting, check out my branch on Github:

Currently, the target that works is native c++, not neko. I’m getting an error in Neko every time I try to do that (maybe it’s too fast and doesn’t like it?)

Let’s imagine currentTime is 2. What’s the remainder of 2 divided by 500? It’s 2, and clearly, 2 >= 1. In other words, if currentTime is any value besides 0, 500, 1000, etc., then this if statement will evaluate to true.

Try if(currentTime >= interval) instead.

1 Like

That did the job. Don’t know how I missed that. Thanks!