Sunday, 18 August 2013

JavaScript setInterval increase and decrease variable

JavaScript setInterval increase and decrease variable

I have a problem with creating a variable that first will increase value
(for example) form 0 to 10, and after that it wil go back from 10 to 0. So
0,1,2,3...10,10,9,8,7...0 (and so on) The main idea looks like this:
var count = 10;
var counter = setInterval(timer, 500);
function timer() {
count = count-1;
if (count == 0) {
count = 10;
}
}
console.log(counter);
But it will only go from 0 to 10 all the time. How to make that 'comeback'
thing? Thank you for help.

No comments:

Post a Comment