2011-08-25 28 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