Implementing a simple timer

classic Classic list List threaded Threaded
7 messages Options
Jan Teske Jan Teske
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Implementing a simple timer

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
Teleplacer Teleplacer
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing a simple timer

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
Sean P. DeNigris Sean P. DeNigris
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing a simple timer

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
dcorking dcorking
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing a simple timer

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
glenpaling glenpaling
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing a simple timer

This post has NOT been accepted by the mailing list yet.
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.
glenpaling glenpaling
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing a simple timer

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).
Sean P. DeNigris Sean P. DeNigris
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Implementing a simple timer

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).
Loading...