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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:21:36+00:00 2026-05-25T03:21:36+00:00

My question is about Javascript. I have a Callback function which receives a Position

  • 0

My question is about Javascript.
I have a Callback function which receives a Position object on a successful callback.

The problem is that when I try to set the properties of the Position object to a global variable at a successful callback it just does not let me do it and the global just remains undefined.

As a workaround to that instead of directly setting the object properties to global variables i’m trying to return it through the callback function but I couldn’t find a way to set the return value of the callback function to a global variable.

Here’s the simplified code.

var x;

navigator.geolocation.getCurrentPosition(onSuccess, onError);

//on Successful callback receives a Position Object
function onSuccess(position) {  
  var coords = position.coords;  
    x=coords;       // Setting directly to an object does not work x still remains undefined after succesful callback               
return coord; // Trying to set this to a global object 

}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}
  • 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-25T03:21:37+00:00Added an answer on May 25, 2026 at 3:21 am

    You can’t return a value from the callback (in this case). That would mean that inside of getCurrentPosition, the return value from the callback has to be assigned somewhere.

    Assigning it to a global variable works, but at the time you access that variable, it was not assigned the new value yet. E.g.

    // x is assigned in `onSuccess`
    var x;
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
    alert(x); // will be undefined, the response is not processed yet
    

    Think about it: getCurrentPosition is probably doing an Ajax request to get the position. Such a request takes (a) time (couple of milliseconds) and because of that (b) is asynchronous, which means that JavaScript does not wait until the response is received. Your code is way faster. onSuccess was not called yet when you alert x.

    Only solution:

    All the code that has to access the position as to be in or called from the callback.

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

Sidebar

Related Questions

I have a question about the javascript. I write a function in the javascript
I have a question about javascript's protected memory. If I have a function done
I just have a question about how to achieve DRY with javascript that generates
I have a jquery question about selecting Id. Basically, I call a javascript function
I am making a webapp. I have a fairly basic question about javascript performance.
There have been some questions about whether or not JavaScript is an object-oriented language.
I have a general question about the design of JavaScript Libraries. I am trying
This is a newbie question: I have a pre-existing function that I would like
I have a question about Javascript widgets. The widget I am working on simply
I want to ask a question about the JavaScript event. I have an <input

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.