简体   繁体   中英

How do I make this button to call the function only once?

I have a button on my HTML page that calls a function to print numbers from JavaScript. I want the button to only execute the function once. Right now it can execute as many times as you click the button and that ruins the output on the page. I tried to empty the array after the function prints numbers but it didn't work as the function would just print 50 then.

Here is my code:

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

Add a variable called something like isClicked outside the function scope and check the variable in the function before doing anything

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

I would suggest not to call click function in HTML template instead you can achieve the result in pure javascript.

Just use addEventListener and removeEventListner to the button click.

 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>

Just remove the event listerner, no need for additional variables.

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

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