Implementing a simple timer

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Implementing a simple timer

Jan Teske
I'm trying to write a simple timer using a TextMorph and its step
method. It should count up every 1 second so I've set stepTime to 1000.
My step method looks as follows:

     step
         self time: self time + 1.
         self contents: self time asString

That should work. But I discover one problem: The timer counts up but it
does so way to fast. If I print the numbers onto the Transcript
everything works fine. So I think the problem lies within the TextMorph.
It seems that during the sent of 'self contents:' the step method is
called again so that it is executed long before the stepTime is up.

Am I right with that suspicion? Can anyone think of a way to work around
my problem?

Thanks in advance!
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Implementing a simple timer

Andreas.Raab
There is absolutely no guarantee how often #step will actually be
called. The given #stepTime is just a recommendation to the system. To
make your code work you should use something like:

MyTimer>>startTimer
        "Start the timer"
        timerStarted := Time totalSeconds.

MyTimer>>step
        "Update my timer"
        self time: Time totalSeconds - timerStarted.

Cheers,
   - Andreas

On 12/15/2011 23:51, Jan Teske wrote:

> I'm trying to write a simple timer using a TextMorph and its step
> method. It should count up every 1 second so I've set stepTime to 1000.
> My step method looks as follows:
>
> step
> self time: self time + 1.
> self contents: self time asString
>
> That should work. But I discover one problem: The timer counts up but it
> does so way to fast. If I print the numbers onto the Transcript
> everything works fine. So I think the problem lies within the TextMorph.
> It seems that during the sent of 'self contents:' the step method is
> called again so that it is executed long before the stepTime is up.
>
> Am I right with that suspicion? Can anyone think of a way to work around
> my problem?
>
> Thanks in advance!

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Implementing a simple timer

Sean P. DeNigris
Administrator
Teleplacer wrote
There is absolutely no guarantee how often #step will actually be
called. The given #stepTime is just a recommendation to the system.
I created an issue and uploaded a new method comment.
http://bugs.squeak.org/view.php?id=7683
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Implementing a simple timer

dcorking
Sean wrote:
>> There is absolutely no guarantee how often #step will actually be
>> called. The given #stepTime is just a recommendation to the system.
>
> I created an issue and uploaded a new method comment.
> http://bugs.squeak.org/view.php?id=7683

I am not sure this is the bug in Jan's morph. "setTime" is a minimum
time, according to the Morphic white paper(*), but Jan's morph runs
too fast.

A WatchMorph also has setTime = 1000, and its WatchMorph>>step is only
called once a second.

Nevertheless, Andreas's suggestion is better than debugging Jan's method.

David

* http://coweb.cc.gatech.edu:8888/squeakbook/uploads/morphic.final.pdf page 13
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Implementing a simple timer

glenpaling
In reply to this post by Jan Teske
Looks like you're on the right track. Put a halt in your stepTime method, you'll find that it's never called.
Reply | Threaded
Open this post in threaded view
|

Re: Implementing a simple timer

glenpaling
In reply to this post by Jan Teske
I tried your timer, it steps every 10 ms on my system. You've encountered one of the hazards of subclassing. TextMorph has complex behaviour which your timer inherits. Somehow, it's overriding the stepTime method. You can prove this by putting a halt in the stepTime method, it isn't called.

Rather than subclass TextMorph you could try making you're timer class another, simpler, morph like a RectangleMorph. During initialization, add a TextMorph as a subMorph (see code below). Make the step method update the time and apply it to the contents of the TextMorph.

initialize

        super initialize.
        time := 0.
        textMorph := TextMorph new.
        self addMorph: textMorph.
        textMorph center: (self center).
Reply | Threaded
Open this post in threaded view
|

Re: Implementing a simple timer

Sean P. DeNigris
Administrator
In reply to this post by Jan Teske
glenpaling wrote this reply, but it never posted to the list:
I tried your timer, it steps every 10 ms on my system. You've encountered one of the hazards of subclassing. TextMorph has complex behaviour which your timer inherits. Somehow, it's overriding the stepTime method. You can prove this by putting a halt in the stepTime method, it isn't called.

Rather than subclass TextMorph you could try making you're timer class another, simpler, morph like a RectangleMorph. During initialization, add a TextMorph as a subMorph (see code below). Make the step method update the time and apply it to the contents of the TextMorph.

initialize

        super initialize.
        time := 0.
        textMorph := TextMorph new.
        self addMorph: textMorph.
        textMorph center: (self center).
Cheers,
Sean