Saya pikir saya mengalami masalah dengan event handler yang tidak memulai fungsi untuk memulai pada klik pertama. mengambil dua klik untuk bekerja. kemudian setelah mengklik tombol lebih lanjut, itu seperti klik di belakang sepanjang proses

Saya baru dalam pengkodean HTML js bersama-sama dan menggunakan situs ini untuk menyelesaikan semua masalah saya. namun saya tidak dapat menemukan apa pun yang membantu saya di sini. maaf jika apa yang perlu saya lakukan dijawab dan tidak dapat menemukan posting yang tepat. maaf jika saya menyembelih cara memposting yang benar

Saya mencoba memasukkan kode di kepala dan tubuh dalam kombinasi yang berbeda untuk melihat apakah hasilnya berubah tidak ada yang berhasil

<html>

<head>
  <title>resource farming</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial- 
        scale=1.0">
</head>

<body>
  <p>
    <button id="mine" onclick="mine()">mine</button>
    <input type="text" id="credit" value="10000"></input>
    click mine to gain credit delay to mine is 5seconds
  </p>
  <p>
    <button id="btncoal" onclick="buycoal()">buy coal</button>
    <input type="text" id="coal" value="1000"> </input>
    click buy to gain coal cost (10 credit) delayed 7.5seconds
  </p>
  <p>
    <button id="btnironore" onclick="buyironore()">buy iron ore</button>
    <input type="text" id="ironore" value="1000"></input>
    click buy to gain iron ore (cost 10 credit) delayed 7.5seconds
  </p>
  <p>
    <button id="btnironbar" onclick="buyironbar()">Buy Iron Bar</button>
    <input type="text" id="ironbar" value="1000"></input>
    click buy to gain iron bar (cost 1 coal 1 iron ore)delayed 10seconds
  </p>
  <p>
    <button id="btnsteelbar" onclick="buysteelbar()">buy steel bar</button>
    <input type="text" id="steelbar" value="0"></input>
    click to gain steel bar (cost 5coal 2iron bars) delayed 20 seconds
  </p>
  <script type="text/javascript" language="javascript">
    var credit1 = 10000;
    var coal1 = 1000;
    var ironore1 = 1000;
    var ironbar1 = 1000;
    var steelbar1 = 0;
    var sleep = 0;


    function mine() {

      document.getElementById('mine').disabled = true;
      document.getElementById('credit').value = credit1;
      credit1 = credit1 + 10;
      sleep = setTimeout(enablemine, 5000);
    }

    function enablemine() {
      document.getElementById('mine').disabled = false;
    }


    function buycoal() {

      if (document.getElementById('credit').value > 10) {
        document.getElementById('btncoal').disabled = true;
        document.getElementById('credit').value = credit1;
        credit1 = credit1 - 10;
        document.getElementById('coal').value = coal1;
        coal1 = coal1 + 1;
        sleep = setTimeout(enablebtncoal, 7500);
      } else {
        document.getElementById('credit').value = credit1;

        document.getElementById('coal').value = coal1;

      }

    }

    function enablebtncoal() {
      document.getElementById('btncoal').disabled = false;
    }


    function buyironore() {

      if (document.getElementById('credit').value > 10) {
        document.getElementById('credit').value = credit1;
        credit1 = credit1 - 10;
        document.getElementById('ironore').value = ironore1;
        ironore1 = ironore1 + 1;
        document.getElementById('btnironore').disabled = true;
        sleep = setTimeout(enablebtnironore, 7500);
      } else {
        document.getElementById('credit').value = credit1;

        document.getElementById('ironore').value = ironore1;

      }
    }

    function enablebtnironore() {
      document.getElementById('btnironore').disabled = false;
    }

    function buyironbar() {
      if (document.getElementById('coal').value > 1) {
        if (document.getElementById('ironore').value > 1) {
          document.getElementById('coal').value = coal1;
          coal1 = coal1 - 1;
          document.getElementById('ironore').value = ironore1;
          ironore1 = ironore1 - 1;
          document.getElementById('ironbar').value = ironbar1;
          ironbar1 = ironbar1 + 1;
          document.getElementById('btnironbar').disabled = true;
          sleep = setTimeout(enableironbar, 10000);
        }
      } else {
        document.getElementById('coal').value = coal1;

        document.getElementById('ironore').value = ironore1;

        document.getElementById('ironbar').value = ironbar1;

      }
    }

    function enableironbar() {
      document.getElementById('btnironbar').disabled = false;
    }

    function buysteelbar() {
      if (document.getElementById('coal').value > 5) {
        if (document.getElementById('ironbar').value > 2) {
          document.getElementById('coal').value = coal1;
          coal1 = coal1 - 5;
          document.getElementById('ironbar').value = ironbar1;
          ironbar1 = ironbar1 - 2;
          document.getElementById('steelbar').value = steelbar1;
          steelbar1 = steelbar1 + 1;
          document.getElementById('btnsteelbar').disabled = true;
          sleep = setTimeout(enablesteelbar, 20000);
        }
      } else {
        document.getElementById('coal').value = coal1;
        document.getElementById('ironbar').value = ironbar1;
        document.getElementById('steelbar').value = steelbar1;
      }
    }

    function enablesteelbar() {
      document.getElementById('btnsteelbar').disabled = false;
    }
  </script>




</body>

</html>

Diinginkan: setiap klik mengaktifkan fungsi menerapkan nilai di bidang penambahan pengurangan sesuai dengan pengaturan fungsi

Hasil aktual membutuhkan dua klik untuk berfungsi. juga tampaknya menjadi klik di belakang yang memengaruhi lebih banyak hasil klik daripada yang seharusnya

0
demonica 22 Oktober 2019, 08:58

1 menjawab

Jawaban Terbaik

Tidak ada masalah dengan klik. Ada kesalahan logis dalam kode Anda. Mari kita ambil tombol klik "milikku". Nilai awal adalah "10000" yang Anda atur dalam html. Ketika Anda mengklik "milikku", pertama-tama setel nilai input "10000" saat Anda menginisialisasi nilai credit1=10000. Dan atur nilai input dan kemudian Anda menambahkan 10 ke nilai aslinya. Jadi pada klik pertama nilainya menjadi 10000. Sekarang nilai credit1 = 10000 + 10

Pada klik kedua nilai credit1=10000 + 10 seperti yang Anda tambahkan 10 pada klik sebelumnya.

Silakan baca komentar di fungsi di bawah ini.

function mine() {
document.getElementById('mine').disabled = true;
document.getElementById('credit').value = credit1;  // On first click setting the value to 10000 which is the same as previos value
credit1 = credit1 + 10; // Now you are adding 10. This will not reflect in input box. And will show when you click second time.
sleep = setTimeout(enablemine, 5000);

}

<html>

<head>
    <title>resource farming</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial- 
     scale=1.0">
</head>

<body>
    <p>
        <button id="mine" onclick="mine()">mine</button>
        <input type="text" id="credit" value="10000"></input>
        click mine to gain credit delay to mine is 5seconds
    </p>
    <p>
        <button id="btncoal" onclick="buycoal()">buy coal</button>
        <input type="text" id="coal" value="1000"> </input>
        click buy to gain coal cost (10 credit) delayed 7.5seconds
    </p>
    <p>
        <button id="btnironore" onclick="buyironore()">buy iron ore</button>
        <input type="text" id="ironore" value="1000"></input>
        click buy to gain iron ore (cost 10 credit) delayed 7.5seconds
    </p>
    <p>
        <button id="btnironbar" onclick="buyironbar()">Buy Iron Bar</button>
        <input type="text" id="ironbar" value="1000"></input>
        click buy to gain iron bar (cost 1 coal 1 iron ore)delayed 10seconds
    </p>
    <p>
        <button id="btnsteelbar" onclick="buysteelbar()">buy steel bar</button>
        <input type="text" id="steelbar" value="0"></input>
        click to gain steel bar (cost 5coal 2iron bars) delayed 20 seconds
    </p>
    <script type="text/javascript" language="javascript">
        var credit1 = 10000;
        var coal1 = 1000;
        var ironore1 = 1000;
        var ironbar1 = 1000;
        var steelbar1 = 0;
        var sleep = 0;

        function mine() {
            document.getElementById('mine').disabled = true;
            credit1 = credit1 + 10;
            document.getElementById('credit').value = credit1;
            sleep = setTimeout(enablemine, 5000);
        }

        function enablemine() {
            document.getElementById('mine').disabled = false;
        }

        function buycoal() {
            if (document.getElementById('credit').value > 10) {
                document.getElementById('btncoal').disabled = true;
                credit1 = credit1 - 10;
                document.getElementById('credit').value = credit1;
                coal1 = coal1 + 1;
                document.getElementById('coal').value = coal1;
                sleep = setTimeout(enablebtncoal, 7500);
            } else {
                document.getElementById('credit').value = credit1;
                document.getElementById('coal').value = coal1;
            }
        }

        function enablebtncoal() {
            document.getElementById('btncoal').disabled = false;
        }

        function buyironore() {
            if (document.getElementById('credit').value > 10) {
                document.getElementById('credit').value = credit1;
                credit1 = credit1 - 10;
                ironore1 = ironore1 + 1;
                document.getElementById('ironore').value = ironore1;
                document.getElementById('btnironore').disabled = true;
                sleep = setTimeout(enablebtnironore, 7500);
            } else {
                document.getElementById('credit').value = credit1;
                document.getElementById('ironore').value = ironore1;
            }
        }

        function enablebtnironore() {
            document.getElementById('btnironore').disabled = false;
        }

        function buyironbar() {
            if (document.getElementById('coal').value > 1) {
                if (document.getElementById('ironore').value > 1) {
                    coal1 = coal1 - 1;
                    ironbar1 = ironbar1 + 1;
                    ironore1 = ironore1 - 1;
                    document.getElementById('coal').value = coal1;
                    document.getElementById('ironore').value = ironore1;
                    document.getElementById('ironbar').value = ironbar1;
                    document.getElementById('btnironbar').disabled = true;
                    sleep = setTimeout(enableironbar, 10000);
                }
            } else {
                document.getElementById('coal').value = coal1;
                document.getElementById('ironore').value = ironore1;
                document.getElementById('ironbar').value = ironbar1;
            }
        }

        function enableironbar() {
            document.getElementById('btnironbar').disabled = false;
        }

        function buysteelbar() {
            if (document.getElementById('coal').value > 5) {
                if (document.getElementById('ironbar').value > 2) {
                    document.getElementById('coal').value = coal1;
                    coal1 = coal1 - 5;
                    steelbar1 = steelbar1 + 1;
                    ironbar1 = ironbar1 - 2;
                    document.getElementById('ironbar').value = ironbar1;
                    document.getElementById('steelbar').value = steelbar1;
                    document.getElementById('btnsteelbar').disabled = true;
                    sleep = setTimeout(enablesteelbar, 20000);
                }
            } else {
                document.getElementById('coal').value = coal1;
                document.getElementById('ironbar').value = ironbar1;
                document.getElementById('steelbar').value = steelbar1;
            }
        }

        function enablesteelbar() {
            document.getElementById('btnsteelbar').disabled = false;
        }
    </script>


</body>

</html>

Saya harap ini akan membantu Anda menyelesaikan ini.

0
Sohail Ashraf 22 Oktober 2019, 06:26