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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:00:54+00:00 2026-06-11T07:00:54+00:00

Forgive me if this is a very basic question, but I’m hoping someone here

  • 0

Forgive me if this is a very basic question, but I’m hoping someone here can explain it very clearly for a beginner to JS.

I noticed that I’ve used the same code in two places and therefore made a function to fire. But I want to use a variable that I created in this function to use after it’s been completed. Here’s a code example:

function calculate_animation(direction) {
    var theWidth = parseInt(visibleArea.outerWidth() - 70),
        currentMargin = parseInt(listingsSlider.css('margin-left')),
        hoursVisible = Math.floor(theWidth / itemWidth),
        hoursWidth = itemWidth * hoursVisible,
        newMargin = currentMargin - hoursWidth;
}


forwardBtn.click(function () {
    calculate_animation();

    // animate the slider
    listingsSlider.animate({
        'margin-left': newMargin + 'px'
    });
});

So I created a new variable called newMargin in the calculate_animation function, and I’m trying to use it in the animate function below.

What do I need to do to make this happen?

  • 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-11T07:00:55+00:00Added an answer on June 11, 2026 at 7:00 am

    It is a scope issue. The newMargin variable is defined inside the function and so the scope of the variable begins at the beginning of the function where it defined with var keyword and ends at the end of the function.

    You can use return newMargin to return the calculated value back to the caller.

    See below,

    function calculate_animation(direction) {
            var theWidth        =  parseInt(visibleArea.outerWidth() - 70),
                currentMargin   = parseInt(listingsSlider.css('margin-left')),
                hoursVisible    = Math.floor(theWidth / itemWidth),
                hoursWidth    = itemWidth * hoursVisible,
                newMargin       =  currentMargin - hoursWidth;
    
            return newMargin;
    }
    
    
    forwardBtn.click(function () {
            // animate the slider
            listingsSlider.animate({'margin-left' : calculate_animation() + 'px'});
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a very simple question so please forgive my ignorance, but can
Please forgive this rather basic question, but I'm very new to Java and still
I know this is a very basic question, but forgive me if I did
I know this is a very simple question for you experts,but please forgive me.Im
Windows Forms, VB application... Forgive me in advance for this very elementary question but
Please forgive the very basic nature of this questions - but we all have
Forgive me if this is a stupid question. I am not very experienced with
Forgive me if this is a repeated question but I am having trouble finding
Forgive my ignorance if this is 'a basic question'..... I have a method that
I am very new to programming, so please forgive me if this question seems

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.