如何使用Java按钮获取x播放次数

我对javascript很陌生,并且我正尝试向后工作以弄清楚它是如何工作的。我在网上找到了这段代码,该代码部分使按钮在脱下前先播放了3次:

     <button onclick='buttonFunction(this)'>Click</button>
    <p id="parag">...</p>
<script> var counter = -1;
    var writing = (function() {

          var myArray = ["one ", "two ", "three!"];
          return function() {
            return myArray[counter += 1];
          }
        })();


        function buttonFunction(self) {

          document.getElementById('parag').innerHTML += writing();
          if (counter == 2)
            self.disabled = true;
        }
    </script>

当我运行这段代码时,它的功能与我期望的完全一样,但是,当我尝试将相同的逻辑应用于这段代码时:

<script>


    function play() {
  var video = new Audio('https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3');
  video.play();


// disables the button while playing
var iterations = 0;
    video.addEventListener('play', function () {    
        iterations ++;
            const button = document.getElementById('button');
button.disabled = true;
    }, false);


    video.addEventListener('ended', function () {
        //alert(iterations); 
        if (iterations == 1) {
            const button = document.getElementById('button');
button.disabled = true;
        }else{
           const button = document.getElementById('button');
button.disabled = false;
        }
    }, false);
    }

</script>

</head>

<body>
<input id="button" class="audioo1" type="button" onclick="play()" value="Play Audio"/>
</body>

我知道第二位代码与第一位代码并不完全相似,因为它有点复杂(在播放音频时也需要关闭按钮。但是我会在if语句中迷路。我将迭代值从1更改为任何其他数字,它只是让用户永远按下按钮,为什么第一个代码块起作用而第二个代码块不能起作用?