In JS not strong, help make the counter on it.

It is necessary that by clicking on the link the timer starts and starts counting until the specified time, and after that, for example, the message Helo world

Forgot to say, this counter will run several times without reloading the page.

I would be very grateful for the help.

    3 answers 3

    The simplest: setTimeout

     function hello_world() { alert("Hello, World"); } setTimeout(hello_world, 1000); 

    After 1 second, the message "Hello, World" is displayed.

    • one
      Thanks, but I didn't make friends with setTimeout. And I already found the answer. - Anatoly

    I found the answer to my own question, can someone come in handy.

     function timer() { var seconds = 20; var seconds_timer_id = setInterval(function() { if (seconds > 0) { seconds --; if (seconds < 10) { seconds = "0" + seconds; } $(".seconds").text(seconds); } else { clearInterval(seconds_timer_id); } }, 1000); } 

    And the link clicking on which will start the timer

     <a href="#" onClick="timer();">Запуск таймера</a> 

    And then the countdown will be displayed

     <span class="seconds">20</span> 
    • Please note that each button press will start a new timer, and only one will stop at the end - Grundy

    Well or so, using setTimeout() .
    Enter the number of seconds and the expiration of the time displays " HELLO WORLD "

     var ticks; function startTimer() { ticks = document.getElementById("nTicks").value; letsGo(); } function letsGo() { if(ticks<0) { document.getElementById("tick").innerHTML='HELLO WORLD!'; return; } document.getElementById("tick").innerHTML=ticks; ticks--; setTimeout(letsGo,1000); } 
     <div id="tick"></div> <input id="nTicks"> <a href="#" onclick="startTimer()">НАЧАТЬ</a>