简体   繁体   English

如何在JavaScript中使用不同的参数从不同的地方调用相同的函数,并且仍然保留该函数的变量的旧值?

[英]How to call same function from different places using different parameters in javascript and still retain old values of variables of that function?

I have following code in my geolocation program. 我的地理定位程序中有以下代码。

When user allows device to get the geolocation, the geolocation() function gets called and the geolocation function again propagates the successPosition function with position object that gives latitude and longitude. 当用户允许设备获取地理位置信息时,将调用geolocation()函数,并且该geolocation函数再次传播successPosition函数与提供纬度和经度的position对象。 Inside the successPosition function I also need other variable called btype (Blood type) since I am developing a program to find nearest blood doners. successPosition函数内部,我还需要另一个名为btype (血液类型)的变量,因为我正在开发一个程序来查找最近的献血者。

So in the past I tried to ask user which blood type to search using javascript prompt method but using that approach user would have to manually type the blood type but what I want is to let user select the type from dropdown. 因此,在过去,我尝试询问用户使用javascript prompt方法搜索哪种血型,但是使用该方法,用户将不得不手动键入血型,但我要让用户从下拉列表中选择类型。 But If I create a form and ask user the type and pass object to SuccessPosition function, the position object becomes undefined - this is because this time we are only passing btype and we dont have position . 但是,如果我创建一个表单并询问用户类型并将对象传递给SuccessPosition函数,则position对象将变为未定义-这是因为这一次我们仅传递btype ,而没有position And when position object gets passed the another variable btype is undefined. position对象被传递时,另一个变量btype未定义。

Please tell me what is the best approach to solve the problem. 请告诉我什么是解决问题的最佳方法。

geolocation: function() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(this.successPosition, this.failurePosition, {
      enableHighAccuracy: true,
      timeout: 3000,
      maximumAge: 4000
    });

  } else {
    return (false);
  }
},
successPosition: function(position) {
  self.latitude = position.coords.latitude;
  self.longitude = position.coords.longitude;

  //self.btype = btype;

  self.partial_url = ("/users?utf8=✓&longitude=" + (self.longitude) 
                   + "&latitude=" + (self.latitude)
                   + "&btype=" + encodeURIComponent(self.btype));

  window.location = self.partial_url;

}

Your successPosition function doesn't have to be responsible for setting the window.location immediately. 您的successPosition函数不必负责立即设置window.location I think the simplest thing in your case would be to have successPosition trigger the form which allows the user to select their blood type, and have an event listener bound to the form element which calls something like setWindowLocation with the blood type when the user selects one. 我认为,在您的情况下,最简单的方法是让successPosition触发表单,该表单允许用户选择其血液类型,并且将事件侦听器绑定到form元素,当用户选择一个血液类型时,该事件侦听器将调用具有血液类型的setWindowLocation之类的东西。 Now you'll have your geolocation and your blood type. 现在,您将拥有地理位置和血型。

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

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