I have an electron app where clicking a button runs a mining function, which takes a long time to run. I am trying to display the nonce as it changes as an element on the page. However, when I run the function, the page freezes and only changes when the function is finished.
//Mining function
function mine(index, time, prev, transactions, difficulty) {
if (index !== 0) {
this.log(`Mining Block ${index}`);
}
const startTime = Date.now();
let nonce = 0;
let hash = '';
while (hash.substring(0, difficulty) !== Array(difficulty + 1).join('0')) {
nonce++;
hash = crypto
.createHash('sha256')
.update(
index.toString() +
time.toString() +
prev.toString() +
transactions.toString() +
nonce.toString()
)
.digest('hex')
.toString();
//Nonce indicator
$("#nonce").text(`Nonce: ${nonce}`);
}
const performanceTime = Date.now() - startTime;
if (performanceTime <= 60000 && index !== 0) {
this.difficulty++;
this.log(`Difficulty Increased. Difficulty Is Now ${this.difficulty}`);
}
const seconds = performanceTime / 1000;
const performance = Math.floor((10 * nonce) / seconds) / 10000;
if (index !== 0) {
if (performance !== Infinity && performance >= 25) {
this.log(`Performance: ${performance} kh/s`);
$("#performance").text(`Performance: ${performance} kh/s`);
} else {
this.log(`Performance Could Not Be Measured`);
$("#performance").text(`Performance: Error`);
}
this.log(`Block ${index} Successfully Mined`);
}
return nonce;
}
//Call
function mineHandler(){mine(props)}
$("#miningBtn").click(mineHandler);