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 6173915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:39:30+00:00 2026-05-23T23:39:30+00:00

I have a couple of fairly simple javascript functions which animate the transition of

  • 0

I have a couple of fairly simple javascript functions which animate the transition of a number, going up and down based on user actions. There are a number of sliders on the page which within their callback they call recalculateDiscount() which animates the number up or down based on their selection.

var animationTimeout;

// Recalculate discount
function recalculateDiscount() {
    // Get the previous total from global variable
    var previousDiscount = totalDiscount;
    // Calculate new total
    totalDiscount = calculateDiscount().toFixed(0);
    // Calculate difference
    var difference = previousDiscount - totalDiscount;
    // If difference is negative, count up to new total
    if (difference < 0) {
        updateDiscount(true, totalDiscount);
    }
    // If difference is positive, count down to new total
    else if (difference > 0) {
        updateDiscount(false, totalDiscount);
    }
}

function updateDiscount(countUp, newValue) {
    // Clear previous timeouts
    clearTimeout(animationTimeout);
    // Get value of current count
    var currentValue = parseInt($(".totalSavingsHeader").html().replace("$", ""));
    // If we've reached desired value, end
    if (currentValue === newValue) { return; }
    // If counting up, increase value by one and recursively call with slight delay
    if (countUp) {
        $(".totalSavingsHeader").html("$" + (currentValue + 1));
        animationTimeout = setTimeout("updateDiscount(" + countUp + "," + totalDiscount + ")", 1);
    }
    // Otherwise assume we're counting down, decrease value by one and recursively call with slight delay
    else {
        $(".totalSavingsHeader").html("$" + (currentValue - 1));
        animationTimeout = setTimeout("updateDiscount(" + countUp + "," + totalDiscount + ")", 1);
    }
}

The script works really well for the most part however there are a couple of problems. Firstly, older browsers animate more slowly (IE6 & 7) and get confused if the user moves the slider again whilst it is still within the animation.

Newer browsers work great EXCEPT for on some occasions, if the user moves the slider mid-animation, it seems that it starts progressing in the wrong direction. So for updateDiscount() gets called with a new value and a directive to count up instead of down. As a result the animation goes the wrong direction on an infinite loop as it will never reach the correct value when it’s counting in the wrong direction.

I’m stumped as to why this happens, my setTimeout() experience is quite low which may be the problem. If I haven’t provided enough info, just let me know.

Thank you 🙂

  • 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-23T23:39:31+00:00Added an answer on May 23, 2026 at 11:39 pm

    OK think it’s fixed…

    Refactored code a little bit, here’s final product which looks to have resolved bug:

    var animationTimeout;
    
    function recalculateDiscount() {
        var previousDiscount = parseInt(totalDiscount);
        totalDiscount = parseInt(calculateDiscount());
        if (($.browser.msie && parseFloat($.browser.version) < 9) || $.browser.opera) {
            $(".totalSavingsHeader").html("$" + totalDiscount);
        }
        else {
            if (previousDiscount != totalDiscount) {
                clearTimeout(animationTimeout);
                updateDiscount(totalDiscount);
            }
        }
    }
    
    function updateDiscount(newValue) {
        var currentValue = parseInt($(".totalSavingsHeader").html().replace("$", ""));
        if (parseInt(currentValue) === parseInt(newValue)) {
            clearTimeout(animationTimeout);
            return;
        }
        var direction = (currentValue < newValue) ? "up" : "down";
        var htmlValue = direction === "up" ? (currentValue + 1) : (currentValue - 1);
        $(".totalSavingsHeader").html("$" + htmlValue);
        animationTimeout = setTimeout(function () { updateDiscount(newValue); }, 5);
    }
    

    Will give points to both Ibu & prodigitalson, thank you for your help 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of pages on my site where fairly large chunks of
I'm using Ruby on Rails. I have a couple of models which fit the
I've been doing some simple web programming using python, and I have a basic
I am going to attempt to keep this as simple as possible, but the
I have a fairly memory intensive process that can cause the heap size to
The C language's bitfields provide a fairly convenient method of defining arbitrary-width fields within
I am currently in the planning stages for a fairly comprehensive rewrite of one
I have been studying the Qt Quarterly article about QGraphicsScene and OpenGL for the
I'm having a go at writing my own toy OS and for the moment
I'm asking for input on a tactical question: I am providing a master block

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.