首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >要等到某个条件为真?

要等到某个条件为真?
EN

Stack Overflow用户
提问于 2011-08-25 23:40:08
回答 7查看 74.8K关注 0票数 24

我在JavaScript中使用navigator.geolocation.watchPosition,我想要一种方法来处理这样一种可能性,即用户可能会在watchPosition找到自己的位置之前提交依赖于位置的表单。

理想情况下,用户会定期看到“等待位置”消息,直到获得位置,然后表单才会提交。

但是,考虑到它缺少wait函数,我不确定如何在JavaScript中实现它。

当前代码:

代码语言: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...
});
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2011-08-25 23:45:59

您可以使用超时来尝试重新提交表单:

代码语言:javascript
复制
$('#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...
    }
});
票数 8
EN

Stack Overflow用户

发布于 2013-02-11 20:16:49

就我个人而言,我使用封装setTimeout()waitfor()函数

代码语言:javascript
复制
//**********************************************************************
// 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()函数:

代码语言:javascript
复制
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 !');
});
票数 29
EN

Stack Overflow用户

发布于 2011-08-25 23:43:40

您将希望使用setTimeout

代码语言:javascript
复制
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会查到你的位置。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7193238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档