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

5th Feb 2021, 1:49 PM
Ginfio
Ginfio - avatar
2 Answers
+ 2
https://code.sololearn.com/WvCLvZ3HlY0e/?ref=app
5th Feb 2021, 2:25 PM
visph
visph - avatar
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
5th Feb 2021, 1:53 PM
Ginfio
Ginfio - avatar