gpt4 book ai didi

javascript - 等待函数结束

转载 作者:行者123 更新时间:2023-11-30 14:25:27 25 4
gpt4 key购买 nike

我有这个代码:

function getLocation() {
//function executed if started by webview
if (typeof Jinterface != 'undefined') {
Jinterface.displayGPSRequest();
}

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError, {
enableHighAccuracy: true
});
} else {
alert("Geolocation is not supported by this browser.");
}
}

在继续代码之前,我需要 JavaScript 等待函数 Jinterface.displayGPSRequest() 结束。我尝试了 async/await 但是作为从 Android Studio 中的 Java 文件调用的函数(我有我的网站的 webview)我不能用它命名async 语句,因为 Java 无法识别它。有帮助吗?

Java函数:

 @JavascriptInterface
public void displayGPSRequest() {
Context context = getApplicationContext();
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API).build();
googleApiClient.connect();
final String TAG = "YOUR-TAG-NAME";
final int REQUEST_CHECK_SETTINGS = 0x1;

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(10000 / 2);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);

PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
Log.i(TAG, "All location settings are satisfied.");
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

try {
// Show the dialog by calling startResolutionForResult(), and check the result
// in onActivityResult().
status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
break;
}
}
});
}

最佳答案

displayGPSRequest 需要提供一种知道何时完成的方法。如果当前没有,您需要对其进行编辑以添加一个(或使用轮询,这不是一个好主意)。

通常的方式是:

  1. 通过 promise 。

  2. 通过原始回调。

因此,如果 displayGPSRequest 返回一个 promise (或者您将其编辑为):

JInterface.displayGPSRequest()
.then(function() {
// The code that should run when it finishes
})
.catch(function() {
// It failed
});

如果 displayGPSRequest 使用原始回调(或您对其进行编辑),则:

JInterface.displayGPSRequest(function() {
// The code that should run when it finishes
// It should also have some way of telling the callback it failed
});

如果它没有提供一种在完成时通知您的方式并且您不能添加一个,您将不得不使用轮询来解决它产生的一些副作用,这是最后的手段:

var timer = setInterval(function() {
if (/*...the side effect is present and so you know it's done..*/) {
clearInterval(timer);
timer = 0;
// The code that should run when it finishes
}
}, 100); // 100ms = ten times a second, adjust as appropriate
setTimeout(function() {
if (timer) {
// Give up
clearInterval(timer);
timer = 0;
}
}, 5000); // 5000ms = five seconds

关于javascript - 等待函数结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52016707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com