简体   繁体   English

Javascript异步函数调用:在回调函数中检索调用函数参数

[英]Javascript asynchronous Function Call : Retrieve calling function paramters in callback function

How can I get the user state in Javascript callback function ? 如何在Javascript回调函数中获取用户状态? Like I have a Javascript funciton making an asynchronous call as follows. 就像我有一个Javascript函数进行异步调用,如下所示。 Now In callback function I need to access userstate. 现在在回调函数中我需要访问userstate。 How can I do this? 我怎样才能做到这一点? In Silverlight we have userstate kind of thing. 在Silverlight中,我们有用户类的东西。 Do we have same kind of mechanism in Javascript as well. 我们在Javascript中也有相同的机制吗? Please assist. 请协助。

Note: I dont want to make use of Global variable as Func1() will be executed in a For Loop. 注意:我不想使用Global变量,因为Func1()将在For循环中执行。

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


   function CallBack(result) {

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

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
   }
} 
function Func1() {
   var userState = "someValue";
   geocoder.asyncCall(parameters , CallBack(userState));
}


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM