2011-08-25 12 views
18

我在JavaScript中使用navigator.geolocation.watchPosition,我想要一種方法來處理用戶可能會在watchPosition找到它的位置之前提交依賴於位置的表單的可能性。等到條件成立爲止?

理想情況下,用戶會定期看到「等待位置」消息,直到獲取位置,然後表單將提交。

但是,由於缺少wait函數,我不確定如何在JavaScript中實現此功能。

當前代碼:

var current_latlng = null; 
function gpsSuccess(pos){ 
    //console.log('gpsSuccess'); 
    if (pos.coords) { 
     lat = pos.coords.latitude; 
     lng = pos.coords.longitude; 
    } 
    else { 
     lat = pos.latitude; 
     lng = pos.longitude; 
    } 
    current_latlng = new google.maps.LatLng(lat, lng); 
} 
watchId = navigator.geolocation.watchPosition(gpsSuccess, 
        gpsFail, {timeout:5000, maximumAge: 300000}); 
$('#route-form').submit(function(event) { 
    // User submits form, we need their location... 
    while(current_location==null) { 
     toastMessage('Waiting for your location...'); 
     wait(500); // What should I use instead? 
    } 
    // Continue with location found... 
}); 
+0

異步思考。查找'setTimeout'。 –

+0

異步和遞歸可能 - 遞歸調用setTimeout,直到current_latlng有一些值? – Richard

+0

先理解語言再歸咎於它。當然沒有「缺乏」等待功能。 – jAndy

回答

7

你可以使用一個超時嘗試重新提交表單:

$('#route-form').submit(function(event) { 
    // User submits form, we need their location... 
    if(current_location==null) { 
     toastMessage('Waiting for your location...'); 
     setTimeout(function(){ $('#route-form').submit(); }, 500); // Try to submit form after timeout 
     return false; 
    } else { 
     // Continue with location found... 
    } 
}); 
+0

Ew。不要在字符串中傳遞表達式。 –

+0

它需要是一個字符串,否則超時將得到.submit的值。我想你可以做function(){return $('#route-form')。submit(); }。我認爲這會奏效。 –

+0

我完全不理解你的第一句話。而且,是的,函數表達式是正確的選擇。 :) –

12

你要使用setTimeout

function checkAndSubmit(form) { 
    var location = getLocation(); 
    if (!location) { 
     setTimeout(checkAndSubmit, 500, form); // setTimeout(func, timeMS, params...) 
    } else { 
     // Set location on form here if it isn't in getLocation() 
     form.submit(); 
    } 
} 

...其中getLocation查找您的位置。

+0

您也可以在'gpsFail'中放置標誌,然後在'checkAndSubmit'中檢查並顯示正確的消息。 –

23

就個人而言,我用一個waitfor()函數封裝了setTimeout()

//********************************************************************** 
// function waitfor - Wait until a condition is met 
//   
// Needed parameters: 
// test: function that returns a value 
// expectedValue: the value of the test function we are waiting for 
// msec: delay between the calls to test 
// callback: function to execute when the condition is met 
// Parameters for debugging: 
// count: used to count the loops 
// source: a string to specify an ID, a message, etc 
//********************************************************************** 
function waitfor(test, expectedValue, msec, count, source, callback) { 
    // Check if condition met. If not, re-check later (msec). 
    while (test() !== expectedValue) { 
     count++; 
     setTimeout(function() { 
      waitfor(test, expectedValue, msec, count, source, callback); 
     }, msec); 
     return; 
    } 
    // Condition finally met. callback() can be executed. 
    console.log(source + ': ' + test() + ', expected: ' + expectedValue + ', ' + count + ' loops.'); 
    callback(); 
} 

我用我的waitfor()功能採用以下方式:

var _TIMEOUT = 50; // waitfor test rate [msec] 
var bBusy = true; // Busy flag (will be changed somewhere else in the code) 
... 
// Test a flag 
function _isBusy() { 
    return bBusy; 
} 
... 

// Wait until idle (busy must be false) 
waitfor(_isBusy, false, _TIMEOUT, 0, 'play->busy false', function() { 
    alert('The show can resume !'); 
}); 
+0

不要忘了')'的'WAITFOR結束()'函數 –

+0

THX,正是我所需要的 –

+2

但是,如果有低於最終更多的代碼(又名初始)WAITFOR打電話,不就是代碼繼續執行,直到暫停,因爲它的時間來運行一次計劃setTimeout調用?最初waitfor只是一個函數,它設置setTimeout調用並返回。你的代碼* *觀看直到值的變化,但它不會*塊*,直到值的變化。 –

14

這就是什麼承諾發明和實施(因爲OP問他的問題)。

查看所有各種實現,例如promise_s.org

+3

這是對這個問題的唯一正確答案,它真的很令人擔憂,直到現在還沒有0張選票 –

+11

@HansWesterbeek可能是因爲承諾的概念是一個廣泛的主題。我已經有一種感覺,我應該使用承諾,但這個答案太模糊,無法幫助我。 – Stijn