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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:44:08+00:00 2026-06-14T09:44:08+00:00

This may be pretty simple but it’s stumping me. I’m trying to return a

  • 0

This may be pretty simple but it’s stumping me. I’m trying to return a variable in javascript to another variable outside of the function’s scope. For some reason, the assignment isn’t occurring. Here’s my code:

var time = Math.round(((new Date()).getTime())/1000);

// first call to get data
var power_now = get_data(<%= @user.id %>, time);

function get_data(user_id, timestamp){
  var targetURL = "get/user_data?time_now="+timestamp+"&user_id="+user_id;
  var power = 0;

  $.get(targetURL, function(data){
    power = data[0]['power'];
    alert(power);
  })

  return power;
}

$('body').html('<h1>'+power_now+','+power_then+'</h1>')

If I place alert(power) within the .get function, the value is correct; however if I place it outside of the .get function, the value is 0.

Maybe I’m missing something about scoping in javascript?

Thanks!

  • 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-14T09:44:10+00:00Added an answer on June 14, 2026 at 9:44 am

    get is a synonym for .ajax with some specific parameters set. The first A in AJAX is Asynchronous. This means that the code is not executed immediately, it sends off the request and moves on. Therefore the assignment of that variable does not occur until after the return statement.

    Instead, you should move the code into the callback:

    $.get(targetURL, function(data){
        power = data[0]['power'];
    
        $('body').html('<h1>'+power_now+','+power_then+'</h1>')
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this may sound pretty simple, but still I can not get it
This question may be pretty simple, but i dont find exact answers .. in
I think this may be a pretty simple ActiveRecord problem to solve but if
This question may look pretty simple, but I cannot find how HLSL handles its
this may sound pretty straight forward, but still I want to post this question
This may be a simple question but I can;t find the answer anywhere. Here
I think this is a pretty simple question but I'm having trouble finding the
This may be a basic question but I am pretty new to DDD. I
Pretty simple code, which I may say worked as intended in Xcode 4.1 but
This may be quite simple but I'm rather new to Lambda's so bear with

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.