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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:36:12+00:00 2026-06-12T00:36:12+00:00

I have used jQuery .animate (Inspiration from how to get a div to randomly

  • 0

I have used jQuery .animate (Inspiration from how to get a div to randomly move around a page (using jQuery or CSS) ) to animate a number of divs.
I have always used javascript, not jQuery as a rule so am a little unsure of my footing here. Hence needing the snippet initially.

My code performs exactly as expected but it seems I have a memory leak somewhere, and over time (abut 10 minutes) the browser – (IE, chrome and FF) will accrue about 500MB RAM usage.

I cannot fathom why, as there are no queued instructions – as helped by the setTimeout – and no loading images etc.

(The IF statement is to allow me to at some other time – alter the class to my 50 ‘#z’ divs, and turn off animation.)

Anyone know about debugging these kind of issues? It crashes the browser over time so is essentially useless as a production page as is.
One more thing, FF’s online crash reports are flipping awful, there’s been no ‘dump’ after submitting, and the issue is less pronounced on chrome/IE – so no crash reports available.
Will update as soon as I get something useful.

function animateDiv() {
    for (z = 0; z < 50; z++) {
        var newq = makeNewPosition();
        var oldq = $("#" + z).position()
        var speed = calcSpeed([oldq.left], newq);

        if (document.getElementById(z).class != "b") {
            $("#" + z).animate({
                left: newq
            }, speed, function() {});
        }
    }

    setTimeout(function() {
        animateDiv();
    }, 0.1);
}

function makeNewPosition() {
    var w = $('#content').width() - 25;
    var nw = Math.floor(Math.random() * w);    
    return [nw];
}

function calcSpeed(prev, next) {
    var y = Math.abs(prev - next);
    var greatest = y;
    var speedModifier = 0.1;
    var speed = Math.ceil(greatest / speedModifier);
    return speed;
}
  • 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-06-12T00:36:13+00:00Added an answer on June 12, 2026 at 12:36 am

    In animateDiv(), you are both starting an animation and setting a timeout to start the next animation for 0.1 milliseconds from now. If your animation is longer than 0.1 milliseconds (which it probably is), then your animations will pile up infinitely, eventually killing the browser. Also, setTimeout() takes an integer number of milliseconds, not a decimal number of seconds.

    Since I doubt your intention is to have multiple animations on the exact same object all running at the same time, you should start the next animation for one object using the completion function of the previous animation, not using a timer. This would guarantee that they never pile up. For safety’s sake, you could also issue a .stop(true) on the object before starting the next animation to make sure whatever came before is done/stopped before starting the next animation, again making sure they can never accumulate indefinitely.

    You could use something like this to fix the problem and be safe:

    function animateDiv() {
    
        function animateItem(item) {
            if (!item.hasClass("b")) {
                var newq = makeNewPosition();
                var oldq = item.position()
                var speed = calcSpeed([oldq.left], newq);
                // make sure animations can never accumulate and 
                // then start the next one
                item.stop(true).animate({
                    left: newq
                }, speed, function() {
                    // run next animation on this object when this one finishes
                    animateItem(item);
                });
            }
    
        }
        // start all animations
        for (z = 0; z < 50; z++) {
            animateItem($("#" + z));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used the slideUp/Down functions in Jquery to animate a div on my
I have this Javascript below using jQuery that shows a div on a page
I have used JQuery within my asp.net page. JQuery is working fine. I could
I have used autocomplete in jQuery from this link . In the normal browser
I have 4 textboxes on my page. I used jquery focus and blur functions
I'm having problems selecting the div i want to animate in jquery. I have
I have a div $(#organizer_listings_container) initially centered using CSS margin: 0 auto; which uses
I have used the code from brenelz.com/2009/03/31/build-a-content-slider-with-jquery/ tutorial and have a functional content slider.
I have made a Web page using jquery and php where all files are
All I want to do is have a div move down the page to

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.