0

病从一个例子开始:

jQuery.when(setLocation()).then(function(e) {
    console.log(e);
});

function setLocation(){
    return navigator.geolocation.getCurrentPosition(setLcationSuccess, onGeoError);
}

function setLcationSuccess(position) {
    return position.coords.latitude; // 33.5554444
}

在这个例子e中是未定义的。我需要得到任何setLcationSuccess()回报

我需要setLocation()使用延迟的 obj 运行,所以我知道坐标什么时候准备好

有任何想法吗?

4

1 回答 1

1

尝试

jQuery.when(setLocation()).then(function(e) {
    console.log('a', e);
});

function setLocation(){
    var d = $.Deferred();
    navigator.geolocation.getCurrentPosition(function(position){
        d.resolveWith(this, [position.coords.latitude]);
    }, function(){
        d.reject();
    });
    return d.promise();
}

演示:小提琴

于 2013-08-12T05:03:45.047 回答