为什么这个 javascript 会被调用两次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4627370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:21:16  来源:igfitidea点击:

Why is this javascript getting called twice?

javascriptjquerygeolocation

提问by Brandon

I have a button where I've hooked the onclick to a call to retrieve the users location and then use the location to make another call to retrieve a list of nearby locations. For some reason the geolocation success method is being called twice on the second click of the button.

我有一个按钮,我将 onclick 连接到调用以检索用户位置,然后使用该位置进行另一个调用以检索附近位置的列表。出于某种原因,第二次单击按钮时会调用两次地理定位成功方法。

So I load the page, click the button, allow permission to use my location, and it will make one ajax request to get the nearby locations. This works fine.

所以我加载页面,单击按钮,允许使用我的位置,它会发出一个 ajax 请求来获取附近的位置。这工作正常。

I click the button again, allow permission to use my location (again), it will make one ajax request to get the locations, wait ~2 seconds, and then make another request using the same coordinates without me allowing permission. So the success method has to be getting called twice, but I'm not sure why.

我再次单击按钮,允许使用我的位置(再次),它将发出一个 ajax 请求来获取位置,等待约 2 秒,然后在没有我允许的情况下使用相同的坐标发出另一个请求。所以成功方法必须被调用两次,但我不知道为什么。

$("#FindLocation").click(function () {
  myScript.findNearbyLocations(displayData, displayError);
});

This click function is not being called twice and I've commented out all the code in displayData and displayError.

这个点击函数没有被调用两次,我已经注释掉了 displayData 和 displayError 中的所有代码。

findNearbyLocations: function (onSuccess, onFailure) {
  if (navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function (position) {
      alert('This is getting called twice except on the initial call.');
      myScript.findStores(position.coords.latitude, position.coords.longitude, onSuccess, onFailure);
    }, function () {
      onFailure(true);
    });
  }
}

Anyone see where I've gone wrong?

有人看到我哪里出错了吗?

Edit: I don't think the markup is the problem. I'm only using basic webpages for testing. There is no styling or other elements at the moment.

编辑:我不认为标记是问题所在。我只使用基本网页进行测试。目前没有样式或其他元素。

<form id="form1" runat="server">
  <div>      
    <MyControls:Search ID="Search" runat="server" />
  </div>
</form>

and the user control (along with all the necessary javascript includes)

和用户控件(以及所有必要的 javascript 包括)

<script type="text/javascript">
  $(document).ready(function () {
    $("#FindLocation").click(function () {
      myScript.findNearbyLocations(displayData, displayError);
    });
  });
</script>

<input type="button" id="FindLocation" value="Find Location" />
<div id="results">
</div>

采纳答案by wosis

Could be a bug in the getCurrentPositionof whatever client you are using. In that case you could simply set a flag when the success method is called and not allow the findStoresfunction to be called a 2nd time.

可能是getCurrentPosition您使用的任何客户端的错误。在这种情况下,您可以在调用成功方法时简单地设置一个标志,并且不允许findStores第二次调用该函数。

回答by Jop de Klein

I faced the same issue, it seems to be a browser bug (tested in Firefox and Safari). I simply verified whether I already had retrieved the same data within a recent timeframe, similar to what is answered here: navigator.geolocation.getCurrentPosition sometimes works sometimes doesn't

我遇到了同样的问题,这似乎是一个浏览器错误(在 Firefox 和 Safari 中测试过)。我只是验证了我是否已经在最近的时间范围内检索了相同的数据,类似于此处的回答:navigator.geolocation.getCurrentPosition 有时有效有时无效

More questions in Google but no definitive answer: http://www.google.nl/search?q=geolocation.getCurrentPosition+called+twice&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

谷歌中有更多问题,但没有明确答案:http: //www.google.nl/search?q= geolocation.getCurrentPosition+called+twice& ie=utf-8&oe= utf-8&aq=t& rls=org.mozilla: en-US: official&client =火狐-a

回答by CaffGeek

Whenever I have found this happening, it's because I accidentally bound the event to the control twice.

每当我发现这种情况发生时,都是因为我不小心将事件绑定到控件两次。

If the code

如果代码

$("#FindLocation").click(function () {
  myScript.findNearbyLocations(displayData, displayError);
});

happens to be run twice, you will have two identical click events bound, and the action will happen twice.

碰巧运行两次,您将绑定两个相同的点击事件,并且该操作将发生两次。

回答by ChrisH

this IS happening, and it IS happening with predictable frequency; what I've found in an app I'm working on right now is that when an error is raised and not caught, somewhere in some anonymous function that appearsto terminate the current Javascript sessions, that's when it begins happening. It's frustrating and annoying; I know whyit's happening, but I don't know whereit's happening... yet.

这正在发生,而且正在以可预测的频率发生;我在我现在正在开发的一个应用程序中发现的是,当出现错误但未被捕获时,在某个似乎终止当前 Javascript 会话的匿名函数中的某个地方,这就是它开始发生的时候。这令人沮丧和烦人;我知道它为什么会发生,但我不知道它发生在哪里......还没有。

But it is, so just a quick comment that all the "that can't happen" answers aren't terribly accurate or helpful.

但它,所以只是一个简短的评论,所有“不可能发生的”答案都不是非常准确或有用。

Anyway, that's my answer; you may have some inner-function tossing an error that JQuery is quietly eating or something.

无论如何,这就是我的答案;您可能有一些内部函数抛出 JQuery 正在悄悄吃东西的错误。

$.02

$.02