Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 428051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:38:14+00:00 2026-05-12T19:38:14+00:00

I am trying to make a numerical value, say 5000, quickly change to another

  • 0

I am trying to make a numerical value, say 5000, quickly change to another value, say 4000, using JQuery. Right now I do this fine using:

mod(".class",4000,"add");

function mod(id,value,type){
    var numb = $(id).html();
    var current_value = parseInt(numb);
    do {
        if(type == "add")
            increment(id);
        else
            decrement(id);
        current_value = parseInt(numb);
    }while(current_value != value);

    function decrement(id){
        $(id).html(current_value-1);
    }

    function increment(id){
        $(id).html(current_value+1);
    }
}

I know it’s probably not the best way to go about it but what I need for it to do is countdown (or up) the numbers very quickly from the current value to the set value. What I intended with this method was to have a delay using setInterval or setTimeout however that makes the whole script fail pretty badly.

Any advice is appreciated, however I would prefer not to use large plugins for this seemingly simple task.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:38:14+00:00Added an answer on May 12, 2026 at 7:38 pm

    When I ran the code you supplied, I was caught in an infinite loop. At the end of the do loop, you have

    current_value = parseInt(numb);

    but the value of numb is only set at the beginning of the function, so it goes on forever. If you change that to

    current_value = parseInt($(id).html());

    then it works fine. Except that it appears to happen instantly.

    I hacked up a method to achieve the animation using timeouts that seems to work fairly well, but as I’m still fairly new to javascript I don’t know if there is a more efficient approach or not. Just tweak the second param passed to setTimeout to get the desired speed. And if you want to change the increment/decrement value, just change the deceleration of dir.

    function mod2(id, value) {
        var numb = $(id).html();
        var current_value = parseInt(numb);
    
        // determine direction to go
        var dir = 1;
        if (current_value - value > 0) {
            dir *= -1;
        }
        getThere(id, current_value, value, dir);
    }
    
    function getThere(id, current_value, target_value, dir) {
        current_value += dir;
        $(id).html(current_value);
        if (current_value != target_value) {
            setTimeout("getThere('"+id+"',"+current_value+","+target_value+","+dir+")", 10);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying make a <dl> to define the icons I am using on the
I have been trying to define multiple combo boxes in R using the tcltk
I'm trying to generate a random 64bit unsigned integer using boost random, but I'm
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
I am working with nHibernate, and trying make sense of bag collections. My data
I've been struggling all day with trying to find classes that converts/decodes ASCII characters
I'd to create some transitions between pages. I've made it this way: $(div.fading).css('display', 'none');
The numpy documentation shows an example of masking existing values with ma.masked a posteriori
My blog's been up for a year or so, and 90% of our traffic
One of my homework questions asked to develop a regex for all strings over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.