javascript. Why doesn't the simplest timer work?



  • Why doesn't this code work? I'm trying to make a simple timer. I want a JavaScript function that, through its variables, transmits the number of seconds and the diva gate where it prints the timer. The function shall divide the number of the first variable by 60 (within minutes and seconds in the balance) into a page, remove the reference number 1 and repeat it with a delay of one second until the variable value is 0.

    I've been testing this option before and after writing the conditions that stop the function, but it doesn't work. What's my mistake?

    <script>
    function timer(tag,txt){ 
    

    document.getElementById(tag).innerHTML+=txt/60 + '<br>';
    txt = txt - 1;

    setTimeout(timer,1000);

    }
    </script>

    <div id="str"></div>

    <input type="button" onclick="timer('str',800)" value="Нажми меня" />



  • <!DOCTYPE html>
    <html>
        <head>
    
        </head>
        <body>
            <script>
                var x='str';
                var time=800;
                function timer(){ 
    
                    document.getElementById(x).innerHTML+=time/60 + '<br>'; 
                    time = time - 5;
    
                    if (time>0) {setTimeout("timer()",100);} else {alert("Computed");} 
    
                }
            </script> 
    
            <div id="str"></div> 
    
            <input type="button" onclick="timer()" value="Нажми меня" />
        </body>
    </html>
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2