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 + '%'; } 

Closed due to the fact that the essence of the question is not clear to the participants Cheg , insolor , αλεχολυτ , default locale , Kromster Sep 18 '17 at 5:45 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Specify what it means to “output” insert the value resulting from the function or insert the function into “div” in the line itself, or (once the battery) is a block that is in another block and occupies the width / height of the container - MedvedevDev
  • and yes, it's not very clear how this function can do anything at all, it's a cycle that does nothing - MedvedevDev
  • display the value of course - Andrei Nikonov
  • one
    you equate percent zero, 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
  • one
    And what value to display? There will be no value ... all the time, setTimeout (battery, 100) will be called; - Igor Lut

1 answer 1

It is not very clear what it is and why it is, but perhaps something like this is required ...

 battery(100, document.getElementById('battery-1')); battery(0, document.getElementById('battery-2')); battery(-100, document.getElementById('battery-3')); function battery( percent, elem ) { if(percent !== 0) { setTimeout(() => { percent < 0 ? battery(++percent, elem) : battery(--percent, elem); }, 100); } elem.innerText = percent + '%'; } 
 <div id="battery-1"></div> <div id="battery-2"></div> <div id="battery-3"></div> 

  • How can I put this into the html div? After all, I have come to court for this, and thanks for the code. - Andrey Nikonov
  • @ Andrey Nikonov, sorry, added - MedvedevDev
  • C'mon, I generally expected that they would answer only tomorrow, thank you very much. - Andrey Nikonov
  • @ Andrey Nikonov, on the SO rule "who managed - he ate", so if you didn’t wait for an answer within 2-3 hours, then most likely it will be only in a month or two)))) And yes, if that’s what You just had to click on the check mark in the upper left corner of the answer to mark the question as "resolved". - MedvedevDev
  • 2
    @AndreyNikonov and where in your question is the part about reaching the value up to 100, and then back to 0? Did you hide her somewhere? Is this a quest? Or casting on the battle of psychics? - Cheg