I have created a fade in function but when i click it very quickly multiple times it start flickering
I dont know if there is a solution to that?
code:
html :
<button id="Fadetest" onclick="FadeTest()">fadetest </button>
<div id="testblock">test</div>
css:
#testblock{
width: 50px;
height: 50px;
background-color: red;
}
function FadeTest() {
const getblock = document.getElementById("testblock");
let opacatiy = 0.1;
getblock.style.opacity = opacatiy;
let fadefunc = setInterval(fade, 5);
function fade() {
if (opacatiy >= 1) {
clearInterval(fadefunc);
} else {
opacatiy = opacatiy + 0.005;
getblock.style.opacity = opacatiy;
}
}
}
I have used 5 miliseconds in the setinterval and a + 0.005 for the opacity
because i want it to fade-in smoothly.
But if there is a better way to achive that please let me know that as well.