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 › Framerate Override
Page Index Toggle Pages: 1
Framerate Override (Read 767 times)
Framerate Override
Apr 30th, 2007, 8:13am
 
Graphics are drawn according to the framerate set, but there are functions which do not depend on that framerate (like void keyPressed() {})

If I wanted to call a function at specified time intervals of 10 milliseconds, how would I do that? Flash has the setInterval() function which does not depend on the framerate. Does Processing have an equivalent?
Re: Framerate Override
Reply #1 - Apr 30th, 2007, 10:52am
 
no, there's no processing equivalent to setTimeout ...

but it can be done. you need to escape the Processing thread (which calls draw) by creating your own thread. here's an example:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1162668310

/F
Re: Framerate Override
Reply #2 - May 2nd, 2007, 7:44am
 
Thank you so much for that pointer....
I've been trying to figure out how to use that code, but even when I set sleep to 10 milliseconds, the function gets called only every 16 milliseconds to 30 milliseconds.

Wish I could execute a function at a sureshot 10 milliseconds everytime!
Re: Framerate Override
Reply #3 - May 2nd, 2007, 8:06am
 
works fine for me ...

Code:

void setup() {
size(640,480);
new MyThread("Test").start();
}
void draw() {
rectMode(CENTER);
noStroke();
fill(0,0,0,15);
rect(mouseX, mouseY, 50, 50);
}
class MyThread
extends Thread
{
int ctr;
MyThread(String str)
{
super(str);
ctr = 0;
}

public void run()
{
while(true)
{
println("run: " + getName() + " " + ctr);
ctr++;
try {
sleep((long)(1000)*10); // 10 sec delay
}
catch (InterruptedException e) {
}
}
}
}


F
Re: Framerate Override
Reply #4 - May 2nd, 2007, 11:19am
 
Unfortunately on any computer it's never possible to guarantee an exact sleep time, due to the nature of processors.
There's always a chance that something else will get start being run just before the 10 milliseconds are up, and not finish until well after. The sleeping thread can't barge it's way in, it has to wait for the operating system to check to see if anything's waiting to be run.

So the sleep call only guarantees a minimum sleep time.
Re: Framerate Override
Reply #5 - May 8th, 2007, 6:56am
 
Sorry fjen, still not able to get the exact 10 millisecond gap. Thought it might be because of the time that Processing uses for displaying println(millis()); But I tried placing the millis() values in an array and printing it out in the end. Got approx the same value as before. Maybe you're able to get the exact 10 milliseconds coz your PC/Mac is faster. Thanks a lot for the help....I guess I'll have to manage with a 50 millisecond gap.

Thanks John....the problem does seem to be like what you stated coz at certain intervals, there's not even a 1 millisecond gap between the function calls.

Re: Framerate Override
Reply #6 - May 8th, 2007, 4:10pm
 
java's timer class, or the swing timer class can handle a timeout. see references i posted here:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1169219266

be aware, however, that you can't just draw at will inside that timeout. you should only draw inside of the draw() method, or methods that are queued, like mousePressed() and keyPressed() (the versions that take no parameters).
Re: Framerate Override
Reply #7 - May 10th, 2007, 8:13am
 
Thanks Fry, I've gone thru some related discussions on the forum and the documentation of the swing class, but being new to Java, I cant figure out whats wrong with my code. Its not printing out the "yes". Please help...

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

void setup()
{
 size(1024,768);background(0);frameRate(15);
}//setup

void draw()
{
print("hi");
}//draw


 ActionListener taskPerformer = new ActionListener() {
     public void actionPerformed(ActionEvent evt)
     {
         //...Perform a task...
         println("yes");
     }
 };
 new Timer(100, taskPerformer).start();
Page Index Toggle Pages: 1