0

I'm new to jQuery, I wanted to change the interval in Setinterval every time it gets executed. But in my code it's executed every 1 sec and not incrementing at all

Here's my code,

 <script>
     var time=1000;
        function myFunction() {

         setInterval(function(){ alert("Hello"); }, time);
         time=time+4000;
    }
  </script>

I think I'm getting this concept wrong, any help with brief explanation where I'm doing wrong will be helpful

Sairam
  • 2,708
  • 1
  • 25
  • 34
Nag
  • 806
  • 1
  • 13
  • 28
  • [Duplicate Already Asked Here][1] how to change interval time at runtime [1]: http://stackoverflow.com/questions/10576106/setintervalfunction-time-change-time-on-runtime – Arun Sharma Jan 15 '17 at 07:08
  • Possible duplicate of [Changing the interval of SetInterval while it's running](http://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-while-its-running) – sargue Jan 15 '17 at 07:46

1 Answers1

3

setInterval is called till its stopped.

What you should use if you want variable timer is setTimeout.

 <script>
     var time=1000;
        function myFunction() {

         setTimeout(function(){ alert("Hello"); myFunction() }, time);
         time=time+4000; // seconds
    }
  </script>
Sairam
  • 2,708
  • 1
  • 25
  • 34