It is necessary to make the addition of numbers to 100 after they fall to 0.
battery(100, document.getElementById('battery')); function battery( percent, elem ) { if(percent !== 0) { setTimeout(() => { percent < 0 ? battery(++percent, elem) : battery(--percent, elem); }, 100); } elem.innerText = percent + '%'; }
percentzero, after which none of the conditions is fulfilled and the function will be called after a timeout and it will be exactly the same ... this is a meaningless function. - MedvedevDev