简体   繁体   English

如何使用 JavaScript 设置函数参数?

[英]How do i set a function parameter with JavaScript?

How do I set a function parameter with JavaScript?如何使用 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中将函数作为参数传递 - How do I pass function as a parameter in javascript 如何为调用 function 的每个单元格设置点击事件,该单元格将单元格的 id 作为 JavaScript 中的参数 - How do I set an on click event for every cell that calls a function which takes id of cell as a parameter in JavaScript Javascript - 如何为函数设置&#39;this&#39;变量 - Javascript - how do i set the 'this' variable for a function 如何在带有普通 javascript 的 object 中将值设置为参数? - How do I set a value as a parameter inside an object with plain javascript? 如何将javascript函数的参数添加到url中? - How do I add the parameter of a javascript function into a url? 如何使用可能未定义的参数创建javascript函数? - How do I create a javascript function with a parameter that may be undefined? 我怎么知道javascript中函数参数的类型 - How do I know the type of a function parameter in javascript 如何获取标签值作为JavaScript函数的参数? - How do I get label value as a parameter into a javascript function? 如何将 C# object 作为参数传递给 Javascript ZC1C425268E68385D14AB5074C17ZA - How do I pass a C# object as the parameter to a Javascript function 如何使用javascript访问在函数中传递的对象参数? - How do I access object parameter passed in a function using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM