increase and decrease t setinterval with localstorage

0

I have some buttons that increase the speed of an automatic sweep that I have programmed, each button increases speed a little, I have it because I do not finish a button that gradually increases the speed and another that reduces it ...

So I have it right now:

function aumentar() {
    clearTimeout(t);
    t = setInterval(clickbutton, 2000);
    localStorage.setItem('duracion', '2000')
}

function aumentar_mas() {
    clearTimeout(t);
    t = setInterval(clickbutton, 1000);
    localStorage.setItem('duracion', '1000')
}

function aumentar_mas_mas() {
    clearTimeout(t);
    t = setInterval(clickbutton, 500);
    localStorage.setItem('duracion', '500')
}

I would like a function that increases the speed for example in 500 and another that would reduce it.

I tried something like that but it does not work:

n = 3000;
function mas() {
n = n - 500;
t = setInterval(clickbutton,n);
localStorage.setItem('duracion', 'n')

would anyone know how to help me?

    
asked by Carlos Zurera Andrés 22.05.2017 в 21:24
source

2 answers

0

In your example you only have to change the following line:

localStorage.setItem('duracion', n);

also put a simple and practical example without using localStorage

var n = 3000;
function modificaTiempo(tiempo){
 n = n + parseInt(tiempo);
 alert(n);
}
<button onclick="modificaTiempo(-50)">
Bajar Tiempo
</button>
<button onclick="modificaTiempo(50)">
Subir Tiempo
</button>
you can save the variable in the localstorage or whatever you want, you just have to change the
n = n + parseInt(tiempo);

for

localstorage.setItem('duracion' , parseInt(localstorage.getItem('duracion')) + parseInt(tiempo));

Practical example of this in link

    
answered by 22.05.2017 в 21:44
0

The following code can be used to replace the 3 routines you have to increase the speed.

Example:

var n = 3000, t = 0;
localStorage.setItem('duracion', 'n')

function mas() {
 clearTimeout(t);
 n = localStorage.getItem('duracion') - 500;
 t = setInterval(clickbutton,n);
 localStorage.setItem('duracion', 'n');
}

With a few small changes you can reuse the routine both to increase and decrease the speed.

Example:

var n = 3000, incremento = 500, t = 0;
localStorage.setItem('duracion', 'n')

function velocidad(incremento) {
 clearTimeout(t);
 n = localStorage.getItem('duracion') + incremento;
 t = setInterval(clickbutton,n);
 localStorage.setItem('duracion', 'n');
}
velocidad(incremento); // para Aumentar la Velocidad
velocidad(-incremento); // para Disminuir la Velocidad
    
answered by 22.05.2017 в 21:50