等待函数结束

Wait for a function to end

我有这个代码:

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.");
  }
}

我需要 Java脚本来等待函数 Jinterface.displayGPSRequest() 结束,然后再继续代码。我试过 async/await 但它是从 Android Studio 中的 Java 文件调用的函数(我有我网站的网络视图)我无法命名它带有 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. 通过承诺。

  2. 通过原始回调。

因此,如果 displayGPSRequest returns 承诺(或您将其编辑为):

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