JSFiddle - React, Tailwind, and code Playground
HTML
<div id="status-display">
<div id="status-display-content">
</div>
</div>
<div id="timer"></div>
<div id="calculation-result"></div>
CSS
#status-display {
background-color: #000;
color: green;
font: 111px Arial, sans-serif;
width: 150px;
height: 150px;
border-radius: 21%;
text-align: center;
line-height: 150px;
}
#timer {
width: 150px;
text-align: center;
font: 15px Arial, sans-serif;
}
#status-display-content {
transition: opacity 1s
}
#calculation-result {
margin-top: 1em;
}
JavaScript
(function() {
let startTimeOfProgram = performance.now(),
timer = 3,
element = $("#status-display-content"),
elementTimer = $("#timer");
setInterval(() => { elementTimer.html((Math.round((performance.now() - startTimeOfProgram) / 10) / 100).toFixed(2)) }, 33);
function updateState() {
element.html(timer--).css({ transition: "none" }).css({ opacity: 1 });
if (timer < 0) timer = 3;
// need to be next cycle because cannot change
// transition and have it animated all in the same cycle
setTimeout(function() {
element.css({ transition: "opacity 1s" }).css({ opacity: 0 }).on("transitionend", () => { updateState(); element.off("transitionend"); });
}, 0);
}
updateState();
function binaryLength(n) {
return n.toString(2).length;
}
const occasionalSleeper = (function() {
let lastSleepingTime = performance.now();
return function() {
if (performance.now() - lastSleepingTime > 33) {
lastSleepingTime = performance.now();
return new Promise(resolve => setTimeout(resolve, 0));
} else {
return Promise.resolve();
}
}
}());
async function asyncBigIntFactorial(n) {
let start = performance.now();
n = BigInt(n);
if (n <= 1n) return 1n;
let result = 1n;
for (let i = 2n; i <= n; i++) {
await occasionalSleeper();
result *= i;
}
console.log("Time taken", (performance.now() - start) / 1000);
return result;
}
let startTimeOfComputation;
setTimeout(function() {
let startTimeOfComputation = performance.now();
asyncBigIntFactorial(60000).then(result => {
$("#calculation-result")
.html(`Number of digits of answer in binary: ${binaryLength(result)}<br>Time it took: ${Math.round((performance.now() -...