如何在用户不点击按钮的情况下播放 javascript 声音?

How can i play a javascript sound without having the user to click a button?

我想在每次事件发生时播放声音效果,而无需用户单击按钮。

例如:

if (blabla > other_blabla){
     var hello = new Audio("this_that.wav");
     hello.play();
}

您需要为您尝试检查的所有元素添加更改事件侦听器

// add change event listener for all elements thath match class "int"
document.querySelectorAll('.int').forEach(int => {
  int.addEventListener('change', event => {
    var int1 = document.getElementById('int1').value;
    var int2 = document.getElementById('int2').value;
    // check if int1 is bigger than int2
    if (int1 > int2) {
      document.getElementById("result").innerHTML = "play audio";
    } else {
      document.getElementById("result").innerHTML = "do not play audio";
    }
  })
})
<label>Integer 1: </label>
<input class="int" type="number" id="int1" value="1">
<label>Integer 2: </label>
<input class="int" type="number" id="int2" value="2">

<p id="result"></p>

尝试将整数 1 更改为大于整数 2 的数字