如何使用 JavaScript 设置函数参数? 我需要为变量设置一个数值,但我似乎无法弄清楚。一些帮助将不胜感激!
// create web audio api context
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// create Oscillator node
var oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.setValueAtTime(255, audioCtx.currentTime); // I want to set 255 to the variable x
oscillator.connect(audioCtx.destination);
oscillator.start();
var x = document.getElementById("HTMLslider").value;
我尝试过这样做:
set oscillator.frequency = var "x"
希望有人能帮忙😁
最佳答案
我猜您想根据 slider 值更改频率:
var slider = document.getElementById("HTMLslider");
// create web audio api context
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// create Oscillator node
var oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.setValueAtTime(Number(slider.value), audioCtx.currentTime); // I want to set 255 to the variable x
oscillator.connect(audioCtx.destination);
oscillator.start();
// Listen for slider changes and apply value to frequency.
slider.addEventListener('change', e => oscillator.frequency.setValueAtTime(Number(e.target.value), audioCtx.currentTime));
<input type="range" min="1" max="1000" value="225" id="HTMLslider" />
关于javascript - 如何使用 JavaScript 设置函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59278020/