0

I am trying to make a simple cookie clicker like game so that when you click the button it starts a while loop that increments the cookie (x) plus the amount of clicker you have every second

	var x = 0;
	var c = 0;

	text = document.querySelector("Header > p");
function Yeet(Add){
	x=x+Add;
	text.innerHTML = x;
}

document.querySelector("header > img").addEventListener("click",Doggo);
function Doggo (){
	Yeet(1);
}
document.querySelector("#clicker1").addEventListener("click",Clicker1);

function Clicker1 () {
	if (x > 9) {
	Yeet(-10);
	c++
	console.log(c);
	}	
}
while (c > 0) {
// statement

setInterval(Yeet(c), 1000);
}
<html>
	<title>
		Cookie
	</title>
			<link rel="stylesheet" href="">
</head>
<body>
	<header>
		<img src="Img/cookie.png" alt="Cookie">
		<p>0</p>
		<button id="clicker1">Clicker 10 cookies 0.1cps</button>
	</header>
	<script src="Script.js"></script>
</body>
</html>

0

1 Answer 1

1

while loop is not necessary and does not await setInterval() call. The code at the question calls Yeet(c) immediately.

You can define the function that will be passed to setInterval, define a variable to reference setInterval(), call clearInterval(reference) if c is equal to 0

function handleInterval() {
  if (c === 0) {
    clearInterval(interval)
  } else {
    Yeet(c)
  }
}

let interval = setInterval(handleInterval, 1000);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.