[ad_1]
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.
Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Asked
Viewed
24 times
<!DOCTYPE html>
<html>
<body>
<p>
The button was clicked
<span class="total-clicks"></span>
times
</p>
<button id="btn1" onclick="addClick()">
Click Me!
</button>
<script type="text/javascript">
let clicks = 0;
let click_power = 1;
let reward_recevied = false;
function addClick() {
clicks = clicks + click_power;
if(clicks >= 10 && reward_received == false) {
click_power = click_power + 1;
reward_received = true;
}
}
function addClick() {
clicks = clicks + 1;
document.querySelector('.total-clicks').textContent
= clicks;
}
// Simulate click function
function clickButton() {
document.querySelector('#btn1').click();
}
</script>
</body>
</html>
\$\endgroup\$
1
If you want to increase the first 10 times the variable clicks + 1, and after 10, clicks + 2 you need to make sure that the names of the variables in the code are “reward_received” everywhere and write the code for the addClick() function once.
In your code you overwrite addClick() function by writing it a second time.
You can view errors in your js if you open html file in the browser by pressing f12.
Here is probably the code you wanted:
<!DOCTYPE html>
<html>
<body>
<p>
The button was clicked
<span class="total-clicks"></span>
times
</p>
<button id="btn1" onclick="addClick()">
Click Me!
</button>
<script type="text/javascript">
let clicks = 0;
let click_power = 1;
let reward_received = false;
function addClick() {
clicks = clicks + click_power;
if(clicks >= 10 && reward_received == false) {
click_power = click_power + 1;
reward_received = true;
}
document.querySelector('.total-clicks').textContent = clicks;
}
</script>
</body>
</html>
\$\endgroup\$
You must log in to answer this question.
Not the answer you’re looking for? Browse other questions tagged .
lang-js
[ad_2]