Count with setTimeout


/ Published in: JavaScript
Save to your folder(s)

Just a counter


Copy this code and paste it in your HTML
  1. var count = 0;
  2. var timer;
  3. var timerOn = false;
  4.  
  5. function timedCount() {
  6. count++;
  7. timer = setTimeout(function(){
  8. timedCount()
  9. }, 1000);
  10. }
  11.  
  12. function doTimer() {
  13. if (!timerOn) {
  14. timerOn = true;
  15. timedCount();
  16. }
  17. }
  18.  
  19. function stopCount() {
  20. clearTimeout(timer);
  21. timerOn = false;
  22. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.