3

How can I get the user state in Javascript callback function ? Like I have a Javascript funciton making an asynchronous call as follows. Now In callback function I need to access userstate. How can I do this? In Silverlight we have userstate kind of thing. Do we have same kind of mechanism in Javascript as well. Please assist.

Note: I dont want to make use of Global variable as Func1() will be executed in a For Loop.

   function Func1() {
       var userState = "someValue";
       geocoder.asyncCall(parameters , CallBack);
   }


   function CallBack(result) {

       // Use result
       // How to access userState in this function
   }

2 Answers 2

5

Try this code:

function Func1() {
   var userState = "someValue";
   geocoder.asyncCall(parameters ,function(){ 
      CallBack(userState);
   });
}


function CallBack(result) {

   // Use result
   // How to access userState in this function
}

update

function PlotAddressOnMap(address) { 
   var address = address; 
   var userState="userState";
   geocoder.geocode({ 'address': address }, CityDetailsReceived(userState)); 
} 

function CityDetailsReceived(userState) {
   return function(results, status){
      //your code
   }
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Though I am getting the userState but I am not getting the expected result now. Actually I am using Google API as follows: // Function to get state details using Google Geocoder function PlotAddressOnMap(address) { var address = address; var userState="userState"; geocoder.geocode({ 'address': address }, CallBack(userState)); } function CityDetailsReceived(userState , results, status ) { } Though I am able to get userState now , but I am not getting the expected result in results and status variables.
4
function Func1() {
   var userState = "someValue";
   geocoder.asyncCall(parameters , CallBack(userState));
}


function CallBack(userState) {
   return function(result){
       // userState is accessible
   }
}

1 Comment

Though I am getting the userState but I am not getting the expected result now. Actually I am using Google API as follows: // Function to get state details using Google Geocoder function PlotAddressOnMap(address) { var address = address; var userState="userState"; geocoder.geocode({ 'address': address }, CallBack(userState)); } function CityDetailsReceived(userState , results, status ) { } Though I am able to get userState now , but I am not getting the expected result in results and status variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.