[英]Wait for a function to end
I have this code: 我有以下代码:
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.");
}
}
I need JavaScript to wait for the function Jinterface.displayGPSRequest()
to end before continuing with the code. 我需要JavaScript等待功能
Jinterface.displayGPSRequest()
结束,然后再继续执行代码。 I tried async
/ await
but being the function called from a Java file in Android Studio (where I've got a webview of my site) I can't name it with async
statement because Java doesn't recognise it. 我尝试了
async
/ await
但是被Android Studio中的Java文件(在我的网站上有一个Webview)调用了该函数,我无法使用async
语句来命名它,因为Java无法识别它。 Any help? 有什么帮助吗?
Java function: 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
will need to provide a means of knowing when it finishes. displayGPSRequest
将需要提供一种知道何时完成的方法。 If it doesn't currently, you'll need to edit it to add one (or use polling, which isn't a good idea). 如果当前不是,则需要对其进行编辑以添加一个(或使用轮询,这不是一个好主意)。
The usual ways are: 通常的方法是:
Via a promise. 通过承诺。
Via a raw callback. 通过原始回调。
So if displayGPSRequest
returns a promise (or you edit it to): 因此,如果
displayGPSRequest
返回一个promise(或您将其编辑为):
JInterface.displayGPSRequest()
.then(function() {
// The code that should run when it finishes
})
.catch(function() {
// It failed
});
If displayGPSRequest
uses a raw callback (or you edit it to), then: 如果
displayGPSRequest
使用原始回调(或您对其进行编辑),则:
JInterface.displayGPSRequest(function() {
// The code that should run when it finishes
// It should also have some way of telling the callback it failed
});
If it doesn't provide a means of notifying you when it finishes and you can't add one, you'd have to use polling for some side-effect it produces, which is very much a last-resort situation: 如果它没有提供通知您何时完成的方法,并且您无法添加它,则必须使用轮询来产生它的某些副作用,这在很大程度上是最后一种情况:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.