我想创建一个振荡器,当用户按下一个键(现在只是空格键)时启动和停止。但是如果按键时间过长,比如一秒钟 - 振荡器会卡住并且不会停止();
let signal = {}
class Beep {
constructor() {
this.audioCtx = new window.AudioContext();
this.osc = this.audioCtx.createOscillator();
this.gainNode = this.audioCtx.createGain();
this.gainNode.connect(this.audioCtx.destination);
this.osc.connect(this.gainNode);
signal.beep = this;
}
}
function startTransmission(e) {
if(e.keyCode == 32) {
let b = new Beep();
signal.beep.osc.start();
}
}
function stopTransmission(e) {
if(e.keyCode == 32) {
signal.beep.osc.stop();
}
}
document.body.onkeydown = (e) => startTransmission(e);
document.body.onkeyup = (e) => stopTransmission(e);