简体   繁体   中英

How do i set a function parameter with JavaScript?

How do I set a function parameter with JavaScript? I need to set a number value to a variable, but i cant seem to figure it out. some help will be much appreciated!

            // 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;

I have tried doing this:

set oscillator.frequency = var "x"

I hope someone can help😁

I'm guessing you want to change the frequency according to the slider value:

 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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM