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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:03:48+00:00 2026-05-26T02:03:48+00:00

I am having issues where I am trying to animate an element. I have

  • 0

I am having issues where I am trying to animate an element. I have a recursive function animate that contains setTimeout to delay the animation. The issue is that the next line after the recursive method is called gets run before the animation is called. For example with this code:

this.slideTo = function(x,y) {
    var originalStyle = this.element.style.cssText;

    var endX = x;
    var endY = y;
    var pos = this.getPosition();
    var startX = pos.x;
    var startY = pos.y; 
    var distX = x - pos.x;
    var distY = y - pos.y;
    var totalDistance = Math.sqrt((distX*distX) + (distY*distY));
    var count = 0;
    var done = false;

    animate(this.element);
    function animate(element) {         
        count += 5;
        var curPos = _(element).getPosition();
        var curDistX =  endX - curPos.x;
        var curDistY = endY - curPos.y;
        var curDistance = Math.sqrt((curDistX*curDistX) + (curDistY*curDistY));
        var percentDone = count/totalDistance;
        var moveToX = Math.round((percentDone*distX) + startX);
        var moveToY = Math.round((percentDone*distY) + startY);
        _(element).moveTo(moveToX,moveToY);
        if(percentDone <= 1) {
            setTimeout(function(){animate(element);},1);
        } else {
            done = true;
        }
    }
    console.log(done);

    this.element.style.cssText = originalStyle;
}

The console displays false because it is run before the animation is done. How can I solve this? btw: this is being called on a mouse event. Could that have anything to do with it?

  • 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-26T02:03:49+00:00Added an answer on May 26, 2026 at 2:03 am

    Try next little changed code:

    this.slideTo = function(x,y) {
        var originalStyle = this.element.style.cssText;
    
        var endX = x;
        var endY = y;
        var pos = this.getPosition();
        var startX = pos.x;
        var startY = pos.y; 
        var distX = x - pos.x;
        var distY = y - pos.y;
        var totalDistance = Math.sqrt((distX*distX) + (distY*distY));
        var count = 0;
        var done = false;
    
        function restoreState(element) {
            element.style.cssText = originalStyle;
            /// ....
        }
    
        function animate(element) {         
            count += 5;
            var curPos = _(element).getPosition();
            var curDistX =  endX - curPos.x;
            var curDistY = endY - curPos.y;
            var curDistance = Math.sqrt((curDistX*curDistX) + (curDistY*curDistY));
            var percentDone = count/totalDistance;
            var moveToX = Math.round((percentDone*distX) + startX);
            var moveToY = Math.round((percentDone*distY) + startY);
            _(element).moveTo(moveToX,moveToY);
            if(percentDone <= 1) {
                setTimeout(function(){animate(element);},1);
            } else {
                done = true;
                restoreState(element);
            }
        }
    
        animate(this.element);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having issues trying to deserializing my xml string that was from a dataset..
I have a connection that works as I can list buckets, but having issues
I'm having issues trying to parse a datetime field that I am returning in
Given that I'm having issues trying to apply an external log4j configuration via log4j.xml
Been having major issues trying to solve this issue, I'll be happy to give
I'm having issues trying to add some dates to a pre-existing class that is
Im having issues trying to join a table twice. I have 3 tables: Users,
I am having issues trying to get the django-debug-toolbar up and running. I have
I am having some issues trying to convert a double to C++ string. Here
I am having issues with trying to convert an UTF-8 string to unicode. I

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.