Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Making sure a counter has finished counting to a number before starting another counter

I have the following code that is two counters, one counts to 30000 then then next counts from there up to a higher number at a different speed.

The problem I have is that the first counter never actually gets to 30000 before the second counter kicks off. I thought that was the whole point of the complete parameter but it's actually just running for 1 second then starting the next counter. But I need it to count up that quickly so I can't change the duration.

How can I make sure the second counter starts only when the first counter has hit 30000?

$({ Counter: 0 }).animate({
  Counter: $('.Single').text()
}, {
  duration: 1000,
  easing: 'linear',
  step: function() {
    $('.Single').text(Math.ceil(this.Counter));
  },
  done: function() {
    checker();
  }
});

function checker() {
  base = parseInt($(".Single").text());
  per_day = 18;
  newtarget = (base + per_day);
  $(".Single").text(newtarget);

  $({ Counter: base }).animate({
    Counter: $('.Single').text()
  }, {
    duration: 20000,
    easing: 'linear',
    step: function() {
      $('.Single').text(Math.ceil(this.Counter));
    },
  });

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="Single">30000</span>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your issue is that step: does not include the "last step".

If you count 1..2..3, it looks like it's 2 steps (starting at 1), but in animation terms, it's actually 1 step (1->2) then finishes with done: (2->3).

Update the UI with counter in the done: callback and it works fine (though the 20000 counter may appear to start too soon, so you may want a setTimeout on checker()).

$('.Single').text(Math.ceil(this.Counter));

$({ Counter: 0 }).animate({
  Counter: $('.Single').text()
}, {
  duration: 1000,
  easing: 'linear',
  step: function() {
    $('.Single').text(Math.ceil(this.Counter));
  },
  done: function() {
    $('.Single').text(Math.ceil(this.Counter));
    checker();
  }
});

function checker() {
  base = parseInt($(".Single").text());
  per_day = 18;
  newtarget = (base + per_day);
  $(".Single").text(newtarget);

  $({ Counter: base }).animate({
    Counter: $('.Single').text()
  }, {
    duration: 20000,
    easing: 'linear',
    step: function() {
      $('.Single').text(Math.ceil(this.Counter));
    },
  });

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="Single">30000</span>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!