jQuery $.post 触发两次?

发布于 2024-09-10 09:12:33 字数 1384 浏览 3 评论 0原文

我有这段代码,并且由于某种原因,根据 Firebug,我的 $.post 函数快速连续触发两次(对于“期刊/天气”)。当我删除“if (navigator.geolocation)”时,它仍然会执行此操作,但是,如果我用 console.log('test') 之类的内容替换 $.post 块,它只会触发一次。

更奇怪的是,当我将 console.log('test') 放在 $.post 函数之前时,我的事件也只触发一次。我假设 jQuery 发生了一些奇怪的事情。有人有什么想法吗?

if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(function(position)
        {
        $.post('journal/weather', {latitude: position.coords.latitude, longitude: position.coords.longitude}, function(result)
            {
            if (typeof(result.current.temp) != 'undefined')
                {
                global.temperature = parseInt(result.current.temp, 10);
                global.temperature_slider.slider('value', global.temperature);
                }
            }, "json");
        });
    }

编辑:好吧,还是很奇怪,有时它会触发两次,有时会触发一次。为什么同一段代码每次执行时会给出不同的结果呢?

编辑2:经过一些实验,我认为这是 Firefox 中的一个错误。为什么?因为这种情况不仅发生在我的页面上,也发生在其他页面上。如果我不断刷新页面,大约前 10 次 navigator.geolocation.getCurrentPosition 会触发回调函数。然而,在某个时刻,它不再这样做了。一旦达到这一点,它就不适用于任何其他使用 getCurrentPosition 的网站。而且这个问题在 Chrome 甚至 IE 上都不会出现。搜索 Google,我发现了这个:

http://groups.google .com/group/mozilla.feedback.firefox/browse_thread/thread/fecc3fb0bad6d0b8

I have this code, and for some reason my $.post function fires twice in quick succession (for 'journal/weather') according to Firebug. It still does this when I remove the "if (navigator.geolocation)", however, if I replace the $.post block with something like console.log('test'), it only fires once.

What's even weirder is when I place console.log('test') before the $.post function, then my event only fires once as well. I am assuming something is going on strange with jQuery. Anybody have any ideas?

if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(function(position)
        {
        $.post('journal/weather', {latitude: position.coords.latitude, longitude: position.coords.longitude}, function(result)
            {
            if (typeof(result.current.temp) != 'undefined')
                {
                global.temperature = parseInt(result.current.temp, 10);
                global.temperature_slider.slider('value', global.temperature);
                }
            }, "json");
        });
    }

EDIT: Okay, stranger still, sometimes it fires twice, sometimes once. How can the same piece of code give different results each time it is executed?

EDIT 2: After some experimentation, I think this is a bug in Firefox. Why? Because it happens not only with my page, but others. If I keep refreshing my page, roughly the first 10 times the navigator.geolocation.getCurrentPosition fires the callback function. However, at a point, it quits doing so. And once it reaches this point, it doesn't work for any other website that uses getCurrentPosition. And this problem never happens on Chrome or even IE. Searching Google, I found this:

http://groups.google.com/group/mozilla.feedback.firefox/browse_thread/thread/fecc3fb0bad6d0b8

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

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

发布评论

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

评论(1

涫野音 2024-09-17 09:12:34

听起来代码可能在页面加载完成之前被调用 - 因此console.log可能尚未初始化。

您的代码是否包含在某种 document.ready 中,例如

$(function () {
if (navigator.geolocation) {
...
}
});

只是一个猜测。需要更多地了解代码似乎有更好的表现的上下文。

希望有帮助,祝你好运。

It sounds like the code may be being called before the page is finished loading - hence console.log perhaps not yet being initialized.

Is your code code wrapped in a document.ready of some sort, e.g.

$(function () {
if (navigator.geolocation) {
...
}
});

Just a guess. Would need to know more about the context in which the code appears to have a better stab at it.

Hope that helps and good luck.

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