-2
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>    
        <script>
         function move(){
  var i =10;
  var demo = document.getElementById('demo3');
  demo.innerHTML = i;

  function run(){
    demo.innerHTML = ++i ;
  }
  var id = setTimeout(run,1000);
}

        </script>
      </head>
      <body>
         <div class="main-content">
             <div class="micro"><p id="demo3"></p></div>
             <input type="button" class="btn" id="start" onclick="move()" name="name" value="Start">
         </div>
      </body>
    </html>

My code is not work properly. It should increase value of i continuously but it stops only after once. Please help.

3 Answers3

1

because setTimeout is not the method you need, indeed it executes a code snippet once after a specified dalay, setInterval might do for your task: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval

iomv
  • 2,409
  • 18
  • 28
1

Change setTimeout to setInterval and it should work.

RealNapster
  • 1,241
  • 1
  • 8
  • 3
1

Here is Fiddle Demo

setTimeout runs the code once. You want to use the setInterval method to run the code repeatedly.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Dhwani sanghvi
  • 475
  • 4
  • 14
  • Please edit the externally hosted code into the post; doing so will make sure it remains useful even if the link breaks. My script [is not allowed to do this](https://meta.stackoverflow.com/a/344512/4751173) because of potential licensing problems. – Glorfindel Nov 20 '21 at 06:51