Loading...
Logo
Processing Forum
Hi all,

I'm having a problem with 'timer' control in GUI for Processing library. Follow me:
Copy code
  1. import guicomponents.*;

    GTimer timer;

    void setup(){
      size(400,300);
     
      timer = new GTimer(this, this, "foo", 10);

    }

    void foo() {
      println("timer!!!");
    }
This code just initializes the ctor and in fact doesn't fire 'foo' routine, as expected. Nothing is displayed on the terminal. Moreover this means that the call to the ctor doesn't actually start the timer, and that's correct. As a matter of fact, if you add timer.start(3) right after the ctor call, it fires three times the 'foo' method.

Copy code
  1. import guicomponents.*;

    GTimer timer;

    void setup(){
      size(400,300);
     
      timer = new GTimer(this, this, "foo", 10);
      timer.setInterval(5000);
      timer.start(3);
    }

    void foo() {
      println("timer!!!");
    }
The problem comes out here: running this code I expected to see "timer!!!" three times each one after 5 seconds (including the first one, of course). But what actually happens is that the first occurrence is displayed after 10 millisecs and the other two after 5 seconds. You can check by changing the interval parameter in ctor call...

Is it a bug?

Replies(8)

When you create a timer with

timer = new GTimer(this, this, "foo", 10);

it does not start auotmatically you have to start it manually with

timer.start(3);
or
timer.start();

When you start the timer it will immediately fire an event and then fire an event based on the interval time you specified. This is not a bug it is just how the GTimer class works. The GTimer class uses the java.swing.Timer class so behaves in the same way.

HTH

Hi quarks,
 When you create a timer with

timer = new GTimer(this, this, "foo", 10);

it does not start auotmatically you have to start it manually with

timer.start(3);
or
 
timer.start();

that was clear to me. If you read better my explanation, you can find it was just the first part, which actually shows no problem.

 When you start the timer it will immediately fire an event and then fire an event based on the interval time you specified.
Ok, but if I specify a different - from ctor call - interval with setInterval() method right after ctor call (please, see my second piece of code) and just before start() method, I'd expect that all the fires come at the interval right specified, including the first one.

Am I wrong?

Instead, my output to the second piece of code is as follows:
10ms     --> timer!!!
5000ms --> timer!!!
5000ms --> timer!!!
I was wrong when I said

When you start the timer it will immediately fire an event and then fire an event based on the interval time you specified.
The value passed to the constructor is the initial delay AND the delay interval. The setInterval method changes the delay interval NOT the initial delay

The solution is to use the delay interval when creating the GTimer object so if you want every an event 5 seconds then use

timer = new GTimer(this, this, "foo", 5000);

this would give

5000ms --> timer!!!
5000ms --> timer!!!
5000ms --> timer!!!


HTH

Hi quarks,

I'm not sure this is the correct behaviour of a timer....

I mean, suppose I need to change the interval because it depends on some other event in the application, but I don't want to start the timer until the right moment. Suppose then, I use the timer as one-shot, using timer.start(1) to start it: the result is that it will fire with the inital delay and not with the last updated interval. And that's what exactly happens to me.

Saying it better, I find more useful a constructor with no initial delay so that I can change the delay as necessary, and then give the start.
Or, as an alternative, the timer should not keep into any account the initial delay if subsequent timer.setInterval(xyz) calls occurr.

Don't you think so?
GTimer is a wrapper class for the java.swing.Timer class and mimics its behaviour.

The only way round it is to create a new GTimer object everytime you want to change the interval. This sounds a strange way to do what you want but I cannot override the Timer class behaviour.


Saying it better, I find more useful a constructor with no initial delay so that I can change the delay as necessary, and then give the start.
Or, as an alternative, the timer should not keep into any account the initial delay if subsequent timer.setInterval(xyz) calls occurr.

Yes it would be nice to be able to choose betwen these behaviours but I am restricted by the java.swing.Timer class.

Hi quarks,

 The only way round it is to create a new GTimer object everytime you want to change the interval. This sounds a strange way to do what you want but I cannot override the Timer class behaviour.
I agree with you, it sounds quite strange. Even because this - de facto - makes setInterval() method useless....

Thanks

Marco
Since I last responded to this topic I have looked at the problem in more detail and discovered that it is possible to set the initial delay and interval times independantly so I have modified GTimer to include methods to do this.

The changes have been included in the  latest release (V1.7.2) of the library.

Hi quarks,

many thanks for your feedback and modifications to the library.
I downloaded the update and with this code:
Copy code
  1. import guicomponents.*;

    GTimer timer;

    void setup(){ 
      timer = new GTimer(this, this, "foo", 5000);
      // SOME OTHER STUFF.....
      timer.setInitialDelay(1000);
      timer.setInterval(1000);
      timer.start(3);
    }

    void foo() {
      println("timer!!!");
  2. }
I'm actually getting the expected output, i.e.
- 1 second
- timer!!!
- 1 second
- timer!!!
- 1 second
- timer!!!