I'm trying to get a jquery function to approach different numbers in the same timeframe in a list by incrementing/decrementing where required. That is I have a list:
// (initialised outside the function scope)
var numbers = [10, 5, 13, 1, 1000];
var curNumber = 0;
var i = 0;
The function should take the same amount of time (e.g. 1 second) to reach from curNumber = 0 to number[0] = 10
as it would from curNumber = 0 to number[4] = 1000
. In the list above, the code would finish each loop in 1 second, from 0 to 10 to 5 to 13 to 1 to 1000 (total 5 seconds if curNumber = 0
initially).
What I'm aiming for is a simple function to run continuously such as:
while(true) {
if (curNumber < numbers[i])
curNumber++;
else if (curNumber > numbers[i])
curNumber--;
if (curNumber == numbers[i]) {
alert(numbers[i] + " number reached!");
i > numbers.length ? i = 0 : i ++;
}
}
A timer button looks like so:
$("#start").click(function() {
myTimerFunction();
});
Should I make use of the var refreshId = setInterval(function() {}, 1000); function? - or is it something that would just run the function each 1000 milliseconds irrelevant of how fast the numbers are approached?