Ask coding questions

← Back to all posts
Error in clicker game.
isaiah08 (63)

I am making a clicker game in Javascript. My problem is that the way i do one clicker per second is:

while (true) {
	!(async function() {
		await p(1000)
	})
	presents += presents_per_second
}

at the end of my code. When i run it, it gets stuck in the forever loop. How can i stop this from happening? Thanks!

Comments
hotnewtop
RYANTADIPARTHI (6010)

Solution

You make a variable for clicks, and then make a click function, then ++ or +=, like this:

click = 0
// make your function, then do a += or ++

Here's a link:

https://stackoverflow.com/questions/21896643/adding-numbers-on-click

Coder100 (17122)

so like you should never ever use a while (true) loop in JS. We use setInterval

setInterval(() => {
  presents += presents_per_second
}, 1000)
Coder100 (17122)

well you put it in a while (true) loop...

isaiah08 (63)

so

while (true) {
	setInterval(() => {
	presents += presents_per_second
	}, 1000)
}

? @Coder100

isaiah08 (63)

It is still not working... @Coder100

Coder100 (17122)

nope, remove the while true @isaiah08

Coder100 (17122)

setInterval will do it infinitely every param milliseconds @isaiah08

isaiah08 (63)

It works now but when i buy a auto clicker it will still not add one every second... @Coder100

Coder100 (17122)

you are passing by argument, that is why. @isaiah08

Coder100 (17122)

remove the argument, so your code becomes:

var presents = 0
presents_per_click = 1
presents_per_second = 0
var setup = function() {
	document.close()
	document.writeln("<h1 style='text-align:center;'>Birthday Clicker!</h1>")
	document.writeln("<h2 style='text-align:center;'>You have " + presents + " presents. </h2>")
	document.writeln('<input type="image" onclick="wrap()" style="display: block; margin-left: auto; margin-right: auto; width: 50%;" src="https://www.al.com/resizer/OYfDsIVi22UJb5iupRtGlDqM7aI=/1280x0/smart/advancelocal-adapter-image-uploads.s3.amazonaws.com/image.al.com/home/bama-media/width2048/img/entertainment_impact/photo/christmas-presents-8d2725e23a3feac8.jpg" alt="Submit" height="400" width="160">')
	document.writeln('<input type="submit" onclick="wrapper()" value="Buy a wrapper: 20 presents">')
	document.writeln('<input type="submit" onclick="auto_wrapper()" value="Buy an auto clicker: 100 presents.">')
	
	setInterval(() => {
	presents += presents_per_second
	}, 1000)
	

}

@isaiah08

isaiah08 (63)

No, try running it and see what happens. @Coder100

isaiah08 (63)

What is wrong with it? @Coder100