Skip to content

Instantly share code, notes, and snippets.

@GoSubRoutine
Last active April 13, 2018 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoSubRoutine/1a0d21828319cf18fecf44101764bd60 to your computer and use it in GitHub Desktop.
Save GoSubRoutine/1a0d21828319cf18fecf44101764bd60 to your computer and use it in GitHub Desktop.
Countdown Class
height: 180
/**
* 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;
}
}
<script async src=http://CDN.JSDelivr.net/npm/p5></script>
<script defer src=countdown.js></script>
<script defer src=sketch.js></script>
/**
* Countdown Class (v1.2.5)
* GoToLoop (2017/Aug/29)
*
* Forum.Processing.org/two/discussion/27733/
* countdown-class-library-for-java-js-python#Item_1
*
* Forum.Processing.org/two/discussion/23846/
* time-delay-in-python-mode#Item_16
*
* Bl.ocks.org/GoSubRoutine/1a0d21828319cf18fecf44101764bd60
*/
"use strict";
const SECS = 7.5, WAIT_TIME = SECS * 1000 | 0,
AWAIT = SECS, END = 'Finished', LF = '\n',
countdown = new Countdown(WAIT_TIME);
let waitingBG, doneBG;
function setup() {
createCanvas(300, 180);
frameRate(60);
colorMode(RGB).blendMode(BLEND);
fill('yellow').stroke(0).strokeWeight(1.5);
textSize(0o100).textAlign(CENTER, CENTER);
waitingBG = color('blue');
doneBG = color('red');
const m = ~~millis(), t = m + WAIT_TIME;
countdown.start();
console.log(m, t, t - m, countdown.toString());
status = m + ', ' + t + ', ' + (t - m);
}
function draw() {
top.document.title = countdown.done && END || AWAIT;
background(countdown.done && doneBG || waitingBG);
const txt = ~~millis() + LF + frameCount;
text(txt, width>>1, height>>1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment