var gain = ctx.createGain(); var levels = [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1]; var levelIdx = 0; var timerId = setInterval(function () { if (levelIdx >= levels.length) { clearInterval(timerId); return; } console.log(levels[levelIdx]); gain.gain.value = levels[levelIdx]; // set gain if (levelIdx === 0) { osc.start(0); osc.stop(ctx.currentTime + levels.length * 0.5); } levelIdx++; }, 500); osc.connect(gain); gain.connect(ctx.destination); IUUQTHJTUHJUIVCDPNLBMJCPSBGCFGGBCB