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

  • Home
  • SEARCH
  • 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 7547659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:30:33+00:00 2026-05-30T09:30:33+00:00

I am trying to work out the correct behavior to string multiple callbacks together.

  • 0

I am trying to work out the correct behavior to string multiple callbacks together.

class Person

  move_head: ->
     head.animate({
        left: x,
     },{
        complete: @move_foot,
     });

  move_foot: ->
     foot.animate({
        left: x,
     },{
        complete: @move_arm,
     });

  move_arm: ->
     arm.animate({
        left: x,
     },{
        complete: something_else,
     });

Now the issue is, head animates fine which calls foot. Foot also animates fine. The problem is that when foot is done, it does not animate the arm. I can’t figure out what the problem could be. I have a guess though that it might have to do with a scoping issue.

  • 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-30T09:30:34+00:00Added an answer on May 30, 2026 at 9:30 am

    Use the fat arrow =>, to bind this to current context:

    move_head: =>
      // ...
    move_foot: =>
      // ...
    move_arm: =>
      // ...
    

    UPDATE: Here’s a short explanation:

    One of the biggest gotchas in javascript is this. In a method (such as your move_head), this is your instance of Person, if you call it as new Person().move_head(). But it doesn’t have to be. For instance, if you call it using new Person().move_head.call(null), this will be null.

    But the biggest gotcha is that when you are inside a function, like a complete callback to a jQuery animate call, this is no longer bound to your object! It is probably bound to window, but not for certain. So what happens in your case is that you give jQuery a reference to @move_foot (or really, this.move_foot). This call seems to work, in that your animation completes. But when jQuery calls that function, it will no longer know that this is supposed to be your Person. So when you tell it to then go to @move_arm, it will look for a method move_arm on some other object (perhaps window).

    A common way to solve this is to save a reference to this before you make the call, like var self = this, and then, inside the callback, refer to that bound variable self instead.

    You could use this technique, there is nothing wrong with it. Something like:

    move_head: ->
      self = this
      head.animate(..., complete: self.move_foot)
    

    But CS does this (binding of this) for you, by giving you the fat arrow: =>.

    The fat arrow says: “Everything inside this method referring to this, shall refer to this object.”

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

Sidebar

Related Questions

I'm trying to work out the correct way to handle populating an array with
I am trying to work out a multiple if-else loop for my code. My
I am trying to work out the best way to replace multiple variables/placeholders within
I have a problem trying to work out the correct algorithm to calculate a
I am trying work out with MERGE statment to Insert / Update Dimension Table
I am trying to work out the best database model for the current setup:
I am trying to work out how to get the value of table cell
I'm trying to work out a way of passing the web current http context
I am trying to work out a code sample to demonstrate the debugging functionality
I am trying to work out the format of a password file which is

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.