0

I need to increase a number and I used setInterval(Function, time) so I put a variable for time: time = 1000 now I need to change it so I put a function that changes it when I click a button:

function changetime() {time = time - 100;}

but it seems that you can't change the time of setInterval while is working... how can I do that? I tried with a setTimeout but the number now changes "jumping". is not regular... I'm not sure but it seems that the "jump" changes when I change the setTimeout time... like if the timeout is now in the setInterval time.

Original code---_>

var time = 1000;
function interval() { setInterval(Function, time);}
function changetime() {setTimeout(interval, 10);tempo = tempo - 200;}
Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Maker
  • 13
  • 6
  • thanks for the help.... now in the console i have that time changes.... from 1000 it changes in 800 but the number still jump.... – Maker Jun 19 '16 at 11:48
  • here's answer for your question: http://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-while-its-running – Marek Woźniak Jun 19 '16 at 11:52
  • i think this post is only for DEcelerating time... i need to speed it up. if you decrease the time for the loop of setInterval it goes faster... – Maker Jun 19 '16 at 12:01
  • ok i solved! it was a better idea to do that with the SetTimeout and then recall it because in settimeout is possible to change time... Thank you anyway.... :D – Maker Jun 19 '16 at 12:27

1 Answers1

1

I had the same problem some time ago, and i made a small function that can do this: https://github.com/Atticweb/smart-interval/blob/master/smart-interval.js

It works like this:

var timer = new timer();
timer.start(function(){
    //more magic here
}, 3000, true);

//change the interval
timer.set_interval(4000);

I hope this helps you, good luck!

Jaapze
  • 732
  • 6
  • 15