我有一个电子应用程序,单击按钮运行挖掘功能,需要很长时间才能运行。我试图在随机数更改为页面上的元素时显示它。但是,当我运行该函数时,页面会冻结并且仅在该函数完成时才会更改。
//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);
慕尼黑8549860
相关分类