I tried using clearTimeout & setTimeout but I get an error as it's not part of the available API. Is there any alternatives I can use for the same thing?
This is what I would do in the javascript web world.
Code: Select all
let timer = null;
const whileHeldDelay = 600;
const controls = initArray(0, 128).map((x, i) => ({
lastPressed: 0,
timer: null,
}));
function onMidi(status, note, velocity) {
const now = Date.now;
clearTimeout(controls[note].timer);
if (isNoteOn(status)) {
controls[note].lastPressed = now;
controls[none].timer = setTimeout(() => {
// do something when held
log('while held', controls[note]);
}, whileHeldDelay);
}
if (isNoteOff(status)) {
if (now - controls[note].lastPressed < whileHeldDelay) {
// do something when quickly pressed
log('quick press', controls[note]);
}
}
}