Timer Daemon

So I’ve implemented an egg timer as a Dial with a Fuse and a Daemon. You set the dial to a number, and that number of turns later the Fuse makes the timer ding. In between that time, the Daemon is supposed to make a ticking noise every turn and decrement the current value of the Dial by 1. I’ve gotten it to do that, but the Daemon keeps going after the Fuse is called. In other words, the timer keeps ticking after it dings, and the current value turns negative. Have I coded the Daemon wrong?

Here’s what I have:

[code]+++ eggTimerDial: Dial, Component ‘dial’ ‘dial’
"The dial is currently at <>. "
minSetting = 0
maxSetting = 60
curSetting = ‘0’

sayDing()
{
    "<p>The timer dings. ";
}

timerFuse = nil 
makeSetting(val) 
{ 
    inherited(val);
    local setting = toInteger(val);
    if(setting!=0)
    {
        timerFuse = new Fuse(self, &sayDing, setting);
        startTimer();
    }
    else if(timerFuse) 
    { 
        timerFuse.removeEvent(); 
        timerFuse = nil; 
        stopTimer();
    } 
} 

startTimer()
{
    timerDaemon = new Daemon(self, &timer, 1);
}

stopTimer() 
{
    if(timerDaemon != nil)
    {
        timerDaemon.removeEvent();
        timerDaemon = nil;
    }
}

timerDaemon = nil

timer()
{
    "<p>The timer is ticking. ";
    local setting = toInteger(curSetting);
    setting--;
    curSetting = toString(setting);
}

;[/code]
Any help is appreciated.

You should probably call stopTimer from sayDing?

Okay so I went back and tried that, and it doesn’t work either.

However, like an hour after posting this thread I actually got it to work by putting an if statement in timer().

timer() { "<p>The timer is ticking. "; local setting = toInteger(curSetting); setting--; curSetting = toString(setting); if(setting==0) { timerDaemon.removeEvent(); timerDaemon = nil; } }
So now I’m wondering why this works, but my previous code didn’t. I was referencing the documentation on this.

At a guess, the difference is due to the order in which end-of-turn events are processed. Possibly your previous code was turning the daemon back on because the setting wasn’t yet 0. Just a guess.