0

i have write this code for perform simple action but not working at all

<script type="text/javascript">
    var a = 10;

    function add() {
        window.setInterval(a2, 10000);

            alert(a);

    }
    function a2() {

        a = a + 2;
    }


</script>

this alert in only returning value 10 only one time. how to achieve this working through timing and looping?

3 Answers 3

2

If you want more alerts, you need to put alert() in the timed function itself. Execution won't return to add() from a2().

function a2() {
    a = a + 2;
    alert(a);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your alert instruction is out of the interval, try the code below:

<script type="text/javascript">
    var a = 10;

    function add() {
        window.setInterval(a2, 10000);
   }
   function a2() {
        a = a + 2;
        alert(a);
   }

</script>

Comments

0

Try this instead:

  var a = 10;

  window.setInterval(add, 1000);

  function add() {
      alert(a);
      a = a + 2;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.