We are about to switch to a new forum software. Until then we have removed the registration on this forum.
/**
* Countdown Class (v1.2.5)
* GoToLoop (2017/Aug/26)
*
* Forum.Processing.org/two/discussion/27733/
* countdown-class-library-for-java-js-python
*
* Forum.Processing.org/two/discussion/23846/
* time-delay-in-python-mode#Item_11
*
* Gist.GitHub.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21
*/
import gotoloop.countdown.Countdown;
static final float SECS = 7.5;
static final int WAIT_TIME = (int) (SECS * 1000);
static final String AWAIT = "Awaiting " + SECS;
static final String END = "Countdown Finished";
static final color WAITING_BG = PImage.BLUE_MASK | PImage.ALPHA_MASK;
static final color DONE_BG = PImage.RED_MASK | PImage.ALPHA_MASK;
final Countdown countdown = new Countdown(WAIT_TIME);
void setup() {
size(300, 180);
smooth(3);
frameRate(60);
colorMode(RGB);
fill(#FFFF00);
textSize(0100);
textAlign(CENTER, CENTER);
final int m = millis(), t = m + WAIT_TIME;
countdown.start();
println(m, t, t - m, TAB, countdown);
}
void draw() {
getSurface().setTitle(countdown.done? END : AWAIT);
background(countdown.done? DONE_BG : WAITING_BG);
final String txt = millis() + "\n" + frameCount;
text(txt, width>>1, height>>1);
}
/**
* Countdown Class (v1.2.5)
* GoToLoop (2017/Aug/26)
*
* https://Forum.Processing.org/two/discussion/27733/
* countdown-class-library-for-java-js-python
*
* https://Forum.Processing.org/two/discussion/23846/
* time-delay-in-python-mode#Item_11
*
* https://Gist.GitHub.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21
*/
package gotoloop.countdown;
import java.util.Timer;
import java.util.TimerTask;
public class Countdown {
protected static final Timer t = new Timer("Countdown");
public TimerTask task;
public int delay;
public volatile boolean done = true;
public Countdown() {
}
public Countdown(final int waitTime) { // milliseconds
delay = Math.abs(waitTime);
}
@Override public String toString() {
return "Delay: " + delay + " - Done: " + done;
}
public Countdown start() {
if (task != null) task.cancel();
done = false;
t.schedule(task = new Timeout(), delay);
return this;
}
protected class Timeout extends TimerTask {
@Override public void run() {
done = true;
}
}
}
Comments
p5.js JS Version:
http://Bl.ocks.org/GoSubRoutine/1a0d21828319cf18fecf44101764bd60
"index.html":
"sketch.js":
"countdown.js":
/** * Countdown Class (v1.2.5) * GoToLoop (2017/Aug/29) * * https://Forum.Processing.org/two/discussion/27733/ * countdown-class-library-for-java-js-python#Item_1 * * https://Forum.Processing.org/two/discussion/23846/ * time-delay-in-python-mode#Item_16 * * http://Bl.ocks.org/GoSubRoutine/1a0d21828319cf18fecf44101764bd60 */ "use strict"; class Countdown { get delay() { return this._delay; } set delay(waitTime) { this._delay = ~~Math.abs(waitTime); } constructor(waitTime=0) { // milliseconds this.delay = waitTime; this.done = true; this._timeout = () => this.done = true; this.task = 0; } toString() { return 'Delay: ' + this.delay + ' - Done: ' + this.done; } start() { clearTimeout(this.task); this.done = false; this.task = setTimeout(this._timeout, this.delay); return this; } }Python Mode Version based on java.util.Timer
https://Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57
"CountdownClass.pyde":
"countdown.py":
""" Countdown Class (v1.2.5) GoToLoop (2017/Aug/28) https://Forum.Processing.org/two/discussion/27733/ countdown-class-library-for-java-js-python#Item_2 https://Forum.Processing.org/two/discussion/23846/ time-delay-in-python-mode#Item_14 https://Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57 """ from java.util import Timer, TimerTask class Countdown: _t = Timer('Countdown') def __init__(self, waitTime=0): # milliseconds self.delay = abs(int(waitTime)) self.done = True class Job(TimerTask): def run(_): self.done = True self._Timeout = Job self.task = None def __str__(self, STR='Delay: %d - Done: %s'): return STR % (self.delay, self.done) def start(self): self.task and self.task.cancel() self.task = self._Timeout() self.done = False self._t.schedule(self.task, self.delay) return selfPython Mode Version based on threading.Timer
https://Gist.GitHub.com/GoToLoop/893dc18856a7842b76a92d8aea837da1
"CountdownClass2.pyde":
"countdown.py":
""" Countdown Class II (v1.2.5) GoToLoop (2017/Aug/30) https://Forum.Processing.org/two/discussion/27733/ countdown-class-library-for-java-js-python#Item_3 https://Forum.Processing.org/two/discussion/23846/ time-delay-in-python-mode#Item_17 https://Gist.GitHub.com/GoToLoop/893dc18856a7842b76a92d8aea837da1 """ from threading import Timer class Countdown: def __init__(self, waitTime=0.0): # seconds self.delay = abs(waitTime) self.done = True def job(): self.done = True self._timeout = job self.task = None def __str__(self, STR='Delay: %.3f - Done: %s'): return STR % (self.delay, self.done) def start(self): self.task and self.task.cancel() self.task = Timer(self.delay, self._timeout) self.done = False self.task.start() return selfThxs for sharing @GoToLoop.
Kf