0
JS how to gradually increase/decrease a value
var x = 1000; setInterval(function(){ x -= 50; }, x) In the above code, I want the interval to take the variable thatâs inside the function, but it wonât work. If I defined the variable inside the function, then it will be undefined. Here are a few real life examples of what Iâm trying to do: - Car is parked. Slowly but surely starts going faster and faster, and faster. - soccer ball is kicked. goes really fast, rolls, slows down, slowly, finally stops. https://code.sololearn.com/WTB2kqV57G1l/?ref=app
2 Answers
+ 2
https://code.sololearn.com/WvCLvZ3HlY0e/?ref=app
0
var x = 1000;
setInterval(func(){
x -= 50;
}, x);
i know the code doesnât work for what iâm trying to make it do, but hereâs the expected output;
every x time, do something;
every 1950 sec do ...
every 1900 sec do ...
every 1850 sec do ...
so by the time itâs 1000 milliseconds, the variable x decreases by 50. So mow it becomes 1950.
then by the time it reaches 1950, decrease again by 50. it becomes 1900. etc