We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey everyone, I wanted to share something I'm working on, but could use some help figuring out one piece of it.
As a Javascript guy I'm really used to using setTimeout(), and find it hard to work through the draw() approach to Processing using the millis() - prevMillis > interval approach, especially when I need to fire off a lot of timers.
So I made a class called SetTimeout(millisAtStart, interval, "func").
In the main file I have an ArrayList where I add to this and remove when finished in the draw() loop.
The piece I can't figure out, because it's Java is the callback function part.
Happy to share the code, but curious if anyone has any immediate feedback regarding the callback function.
Answers
Use the well known command pattern:
Of course, the execute method can be defined to take parameters and to return something, if you need to.
I was wondering the same thing recently and ended up using a wee bit of reflection and a separate thread. Here's a very basic example:
The main tab:
a separate tab I called Utils:
It calls functions in your sketch by passing a string containing their name. You don't need to keep track of intervals as you would normally in javacript, you just need to pass the name of the function the interval called.
This is a basic approach and has it's limitations. For example you can't use arguments/parameters with the functions at the moment.
HTH, George
This is great, thanks George! Obviously the lack of arguments/param is a slight issue, but great start!