如何使此按钮仅调用一次函数?

时间:2019-12-14 14:50:22

标签: javascript html function button

我在HTML页面上有一个按钮,该按钮调用一个函数以从JavaScript打印数字。我希望按钮仅执行一次功能。现在,它可以执行多次,只需单击按钮,它就会破坏页面上的输出。在函数打印数字后,我尝试清空数组,但由于该函数仅打印50个字符,所以无法正常工作。

这是我的代码:

<button type="button" onclick="decimal_table()">Generate Table</button>
<button id = "clear_pos" type="button" onclick="reset_button()">Clear</button>
<script>

  const real_array = [];
  const binary_array = [];
  const octal_array = [];
  const hex_array = [];

  function decimal_table ()
  {


     for (let i = 0; i <= 50; i++)
       {
        real_array.push(i);
        binary_array.push(i.toString(2).padStart(8, 0));
        octal_array.push(i.toString(8));
        hex_array.push(i.toString(16));

        document.getElementById("numbers").innerHTML = real_array.join(" ");
        document.getElementById("binaries").innerHTML = binary_array.join(" ");
        document.getElementById("octals").innerHTML = octal_array.join(" ");
        document.getElementById("hex").innerHTML = hex_array.join(" ");
       }
  }

   function reset_button ()
   {
     for (let i = 0; i <= 50; i++)
       {
        real_array.shift(i);
        binary_array.shift(i);
        octal_array.shift(i);
        hex_array.shift(i);

        document.getElementById("numbers").innerHTML = real_array;
        document.getElementById("binaries").innerHTML = binary_array;
        document.getElementById("octals").innerHTML = octal_array;
        document.getElementById("hex").innerHTML = hex_array;
       }
   } 

</script>

3 个答案:

答案 0 :(得分:3)

在函数作用域之外添加一个名为isClicked之类的变量,并在执行任何操作之前检查函数中的变量

<button type="button" onclick="decimal_table()">Generate Table</button>
<script>
  var isClicked = false;
  const real_array = [];
  const binary_array = [];
  const octal_array = [];
  const hex_array = [];

  function decimal_table ()
  {

      if(!isClicked){
     for (let i = 0; i <= 50; i++)
       {
        real_array.push(i);
        binary_array.push(i.toString(2).padStart(8, 0));
        octal_array.push(i.toString(8));
        hex_array.push(i.toString(16));

        document.getElementById("numbers").innerHTML = real_array.join(" ");
        document.getElementById("binaries").innerHTML = binary_array.join(" ");
        document.getElementById("octals").innerHTML = octal_array.join(" ");
        document.getElementById("hex").innerHTML = hex_array.join(" ");
        isClicked = true;
       }
      }
  }

</script>

答案 1 :(得分:1)

我建议不要在HTML模板中调用click函数,而可以使用纯JavaScript来实现结果。

只需使用addEventListenerremoveEventListner来单击按钮即可。

  const real_array = [];
  const binary_array = [];
  const octal_array = [];
  const hex_array = [];

  const genTable = document.getElementById('generate-table');
  const resetBtn = document.getElementById('clear_pos');
  genTable.addEventListener('click',decimal_table)
  resetBtn.addEventListener('click',reset_button)

  function decimal_table ()
  {

     for (let i = 0; i <= 50; i++)
       {
        real_array.push(i);
        binary_array.push(i.toString(2).padStart(8, 0));
        octal_array.push(i.toString(8));
        hex_array.push(i.toString(16));

        document.getElementById("numbers").innerHTML = real_array.join(" ");
        document.getElementById("binaries").innerHTML = binary_array.join(" ");
        document.getElementById("octals").innerHTML = octal_array.join(" ");
        document.getElementById("hex").innerHTML = hex_array.join(" ");
       }
      genTable.removeEventListener('click',decimal_table);
  }

function reset_button ()
   {
     for (let i = 0; i <= 50; i++)
       {
        real_array.shift(i);
        binary_array.shift(i);
        octal_array.shift(i);
        hex_array.shift(i);

        document.getElementById("numbers").innerHTML = real_array;
        document.getElementById("binaries").innerHTML = binary_array;
        document.getElementById("octals").innerHTML = octal_array;
        document.getElementById("hex").innerHTML = hex_array;
       }
       genTable.addEventListener('click',decimal_table)
   }
<button type="button" id="generate-table">Generate Table</button>
<button id = "clear_pos" type="button" >Clear</button>
<span id="numbers"></span>
<br><br>
<span id="binaries"></span>
<br><br>
<span id="octals"></span>
<br><br>
<span id="hex"></span>

答案 2 :(得分:0)

只需删除事件列表器,就不需要其他变量。

<button type="button" onclick="decimal_table(this)">Generate Table</button>

<script>
  function decimal_table(el) {
    // remove the event
    el.removeAttribute("onclick")
    console.log('clicked')
    
    // ..rest of your code
  }
</script>