Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple jquery second counter

What is the simplest way to increase a variable by 1 every second?

like image 907
Kevin Brown Avatar asked Jun 15 '10 22:06

Kevin Brown


1 Answers

var counter = 0;
setInterval(function () {
  ++counter;
}, 1000);

Additionally, if you ever need to turn it off again, this makes that possible:

var counter = 0;
var myInterval = setInterval(function () {
  ++counter;
}, 1000);

// to stop the counter
clearInterval(myInterval);
like image 148
g.d.d.c Avatar answered Oct 22 '22 22:10

g.d.d.c