I'm trying to make a JavaScript program that simulates the game Cookie Clicker in order to calculate an optimal strategy to reach a goal as fast as possible. Essentially how it works is by the user inputting a few things through a simple website, then using a while loop to repeat through a set of functions until it has reached a goal. This program can take a while to run and I would like to display various statistics that constantly update as the program is running, but I don't know how to do this.
As an example I've made a simple calculator that shows essentially the same issue that I am having:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Summation Calculator</title>
</head>
<body>
<h1>Summation Calculator</h1>
<p>Calculates the sum of every number from 1 to your number.</p>
<label for="number">Number:</label>
<input type="number" id="number" min="0" value="1000000" step="10"><br>
<button id="calculate" onclick="sum(number.value)">Calculate</button><br>
<div id="output"></div>
<script src="main.js"></script>
</body>
</html>
function sum (maxNum){
number = 0;
for (i = 1; i <= maxNum; i++){
number += i;
document.getElementById("output").innerHTML = "Total: " + number;
}
}
I have tried using setTimeout and setInterval, but I don't really know what I'm doing and neither worked.
I made another post earlier about this, but I decided to repost because the other post didn't have any sort of example or anything. There was also another post that I looked at that had a similar problem (here) but I couldn't really modify the solution to my problem.