We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › execute only once
Page Index Toggle Pages: 1
execute only once (Read 704 times)
execute only once
Jun 25th, 2009, 10:40am
 
Im writing a programm that should check for an update every 80 seconds. so i wrote a timer that calculates the seconds with millis()/1000
works fine. Now i want to execute the update everytime seconds%80==0; that works almost because, its trying to update more then once, because seconds%80==0 happens to be true in every frame in this second, so is there an way to execute it only once? Using millies doenst work, cause it skips some of them. Hope its clear.  Thanks for your help Guys!
Re: execute only once
Reply #1 - Jun 25th, 2009, 12:33pm
 
Hi,

i wrote a blog post a while ago showing how to achive extra low framerates using a thread

http://www.local-guru.net/blog/2009/01/29/extra-low-framerates-in-processing

in the setup method set the sketch to noLoop()
and then call redraw from a thread every n seconds
(i use this method in a sketch whith one frame per
30 minutes)

void setup() {
 size(300,300);
 smooth();
 noLoop(); // <- turn off the processing loop

 start( 80 * 1000 ); // <- start the thread
}

void draw() {
 background( 255 );
 // draw some random circles
 for ( int i =0; i< 10; i++) {
   color c = color(random( 255 ));
   fill( c );
   stroke( c );
   int radius = int(random( 100 )+ 40);
   ellipse( random( width ), random( height ), radius, radius );    
 }  
}

void start( final int mil ) { //<- mil has to be final to be accessible in run()
  new Thread() {
  public void run() {
    while( true ) {    
       delay( mil );
       redraw();
     }
  }
}.start();
}

Re: execute only once
Reply #2 - Jun 25th, 2009, 12:37pm
 
Hmm problem is that i am running a normal sketch, with interaction and animation and just want to connect to a database to update data. I guess it wont work then, right?
Re: execute only once
Reply #3 - Jun 25th, 2009, 12:42pm
 
hmmmm, yes
but you could use something similar

remove the noloop from the setup()  method
and put your update code into the thread
instead of the redraw() method.

store the value you are looking for into a variable and access it
from the draw method

then the draw method can run at whatever framerate you like
and the update gets only triggert every 80 sec
Re: execute only once
Reply #4 - Jun 25th, 2009, 1:02pm
 
Usual way is to record current time (say in variable lastTime). When millis() / 1000 - lastTime >= 80, do your action and set lastTime to current time.
Page Index Toggle Pages: 1