A subclass of Java's Timer object. VariableTimer allows for dynamic time intervals, defined by a function. The value returned by the function is the period in milliseconds for each scheduled interval.
Here is an example of a Timer with a linear period. The period will start at one second and increase by one second every interval. This timer will execute after 1 second, 2 seconds, 3 seconds, … etc.
VariableTimer myTimer = new VariableTimer();
myTimer.scheduleAtVariableRate(new TimerTask() {
public void run() {
System.out.println("Executed!");
}
}, new Function() {
public long function(long x) {
return (1000 * x) + 1000;
}
});
Here is an example of a Timer with an exponential period. This timer will execute immediately after 0 seconds, 1 second, 4 seconds, 9 seconds, … etc.
VariableTimer myTimer = new VariableTimer();
myTimer.scheduleAtVariableRate(new TimerTask() {
public void run() {
System.out.println("Executed!");
}
}, new Function() {
public long function(long x) {
return 1000 * x^2;
}
});
Functions such as pause()
and resume()
are also supported.
This code is distributed under the terms and conditions of the MIT license.