简体   繁体   中英

Use value from device orientation in other function

I have a question about using a value I receive from the device orientation event in another function. This is what I have :

$(window).ready(function(){ 
    $("#btnShowDirection").click(showDirection);

    // Device orientation
    if (window.DeviceOrientationEvent) {
        window.addEventListener("deviceorientation", handleOrientation, false);
    } else {
        alert("Device Orientation is not available");
    }
}); // END OF DOCUMENT READY

// orientation object to save heading of the device
var orientation = {};

function handleOrientation(orientData) {
    var alpha = orientData.alpha;
    orientation.value = alpha;
}

var watchProcess = null;  
function showDirection() {
    if (navigator.geolocation) {
        if (watchProcess == null) {
            navigator.geolocation.watchPosition(geoWatchSucces,geoError);  
        }
    } else {
        alert("Geolocation is not supported!");
    }
}

function geoWatchSucces(position) {
    alert(orientation.value);
}

The alert(orientation.value); gives back undefined. How can I fix this? I want to use that variable in my watchprocess success function.

jsfiddle: http://jsfiddle.net/HJTNJ/

Niels

direction.value是在$(window).ready范围内设置的,即在您的geoWatchSucces函数中:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM