Search code examples
javascriptcordovacordova-plugins

latency time personal cordova plugin


I'm currently implementing a plugin for cordova. For now, I just get the angles from the camera and return their values to javascript. Everything works, I got the correct values. But there is a latency time problem in my method "getAngles" to get the angles.

In this code for example, the first alert displayed indicate: "horizontal: 0, vertical: 0" (the second alert in the code) and after this is the alert "horizontal: 62.2, vertical: 39.4" (the first alert, in the "getAngles" method)

Do you know where the problem is? Is there a way to avoid this latency time?

(I just want to directly store the angle values in the "horizontal" and "vertical" variables)

(Maybe there is something to do in the android code? for now the plugin is only available on android)

horizontal = 0;
vertical = 0;

function alertCameraAngles() {
    helloWorld.getAngles(function(result) {
        horizontal = result.horizontalAngle;
        vertical = result.verticalAngle;

        alert("horizontal: " + horizontal + "\nvertical: " + vertical);
    }, function(error) {
        alert("error");
    });

    alert("horizontal: " + horizontal + ", vertical: " + vertical);
}

Solution

  • You cannot avoid this latency, because javascript is asynchronous. When you call getAngles, your plugin is crossing to native side, retrieving some data, etc and returning the result in the callback (as it should). Meanwhile, js code keeps running (not blocking its own execution) and executing the second alert. This is neither wrong nor bad behaviour on the contrary, this is exactly how it should behave.