我有這個代碼,並且出於某種原因,我的$ .post函數根據Firebug快速連續兩次(用於'journal/weather')激發。當我刪除「if(navigator.geolocation)」時仍然會這樣做,但是,如果我用console.log('test')替換$ .post塊,它只會觸發一次。
什麼更奇怪的是當我把$ .post函數放在console.log('test')時,那麼我的事件也只會觸發一次。我假設有什麼奇怪的事情與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的一個bug。爲什麼?因爲它不僅發生在我的頁面上,還發生在其他頁面上。如果我不斷刷新我的頁面,大約是navigator.geolocation.getCurrentPosition的前10次將觸發回調函數。然而,在某一點上,它退出了這一過程。一旦達到這一點,它不適用於任何其他使用getCurrentPosition的網站。而這個問題在Chrome甚至IE上都不會發生。搜索谷歌,我發現這一點:
http://groups.google.com/group/mozilla.feedback.firefox/browse_thread/thread/fecc3fb0bad6d0b8
如果你告訴如何調用這個代碼塊,它可能會有所幫助。並且這個問題可能在你使用'getCurrerentPosition'檢查這篇文章http://stackoverflow.com/questions/2707191/unable-to-cope-with-the-asynchronous-nature-of-navigator-geolocation。 – qw3n 2010-07-16 18:29:41
我相信我正確使用getCurrentPosition ..至少根據您提供的鏈接中的答案。 但是,得到這個。我把這段代碼放到隨機網頁(任何網頁)上的Firebug命令行中: navigator.geolocation.getCurrentPosition(function(){alert('Position obtain');}); 像40秒沒有任何反應,然後我的信息出現。而我只是試過Chrome,我根本沒有得到這個問題。 – Nick 2010-07-16 18:37:53