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

发布于 2024-10-11 01:46:54 字数 1521 浏览 3 评论 0原文

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

因此,我加载页面,单击按钮,允许使用我的位置,然后它将发出一个 ajax 请求来获取附近的位置。这很好用。

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

$("#FindLocation").click(function () {
  myScript.findNearbyLocations(displayData, 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);
    });
  }
}

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

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

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

和用户控件(以及所有必需的 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>

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.

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.

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.

$("#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.

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)

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街女流氓 2024-10-18 01:46:54

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

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

一梦等七年七年为一梦 2024-10-18 01:46:54

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

如果代码

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

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

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.

我喜欢麦丽素 2024-10-18 01:46:54

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

Google 上有更多问题,但没有明确的答案:http:// /www.google.nl/search?q=geolocation.getCurrentPosition+known+twice&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox -a

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

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

¢好甜 2024-10-18 01:46:54

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

但它,所以只是快速评论一下,所有“这不可能发生”的答案都不是非常准确或有帮助。

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

$.02

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 appears to terminate the current Javascript sessions, that's when it begins happening. It's frustrating and annoying; I know why it's happening, but I don't know where it's happening... yet.

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.

$.02

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文