facebook

I need a Break, or a "Pause" or a "Hold"….

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #341878 Reply

    SKnoxmn
    Member

    I’d really like to find some guidance on how to get the device orientation. Still running into the “Internet” where everyone, or a lot of them, seem to think orientation is all about portrait and landscape.

    Anyhow, I’m doing this:
    _________________________________
    function getAngle()
    {
    if (window.DeviceOrientationEvent) {
    console.log(“DeviceOrientation is supported”);
    window.addEventListener(‘deviceorientation’, function(eventData) {
    var Cant = eventData.gamma;
    var Angle = eventData.beta;
    var Dir = eventData.webkitCompassHeading;
    deviceOrientationHandler(Cant, Angle, Dir);
    }, false);
    }
    else
    {
    alert(“Not supported on your device or browser. Sorry.”);
    }

    function deviceOrientationHandler(Cant, Angle, Dir){
    $(‘#m1-Learning-textField4’).val(Angle);
    $(‘#m1-Learning-textField5’).val(Dir);
    }}
    ________________________________________

    And this is working to get the data, and putting in the fields where I want it. I’d like this to run for about 3 seconds and “Stop”. Leaving the data in the data in the fields.

    I’d like to be able to get the data in the fields and be able to use that data in some math functions.

    First part, in my mind, is figuring out how to get this thing to stop…

    SKnox

    #341887 Reply

    Brandon
    Member

    Use the timer function:

    var myVar=setInterval(function(){function_to_run()},3000); // Run function every three seconds

    Stop timer:

    window.clearInterval(myVar);// stop the timer from running

    #341891 Reply

    SKnoxmn
    Member

    I’d like it to run for the 3 seconds, not run every three seconds. 🙂 and then stop running.

    SKnox

    #341903 Reply

    Unknown Author
    Participant

    Remember that javascript is a multithreaded language. Therefore, to truly “halt”, you need a recursive function (go until a variable value changes, etc.). In one of my apps, I use recursion to wait for results to come back from the Google Places API, before displaying a results page.

    -1TC

    PS – If you just want to run for three seconds, then count the number of seconds and stop your loop when you hit 180 seconds. Easy.

    #341904 Reply

    SKnoxmn
    Member

    1TC

    thanks, yeah I kind of figured it was going to be a timer of some kind that run for the duration then stop.

    The problem I’m having is in finding the information on “how to” make it stop. maybe I’ll find a way to change the “false” to true. That might cause it to stop.

    SKnox

    #341924 Reply

    Brandon
    Member

    You could make two timer functions. One that starts it running, and another that will stop it in three seconds.

    #341931 Reply

    SKnoxmn
    Member

    Im still stuck. it looks like if I try to put in a timer then the function displays nothing.

    I can’t find anything (yet) that will show me how to get a “snapshot” of lat lon alt direction and angle. its all about continuous updates and rotating images or putting pointers on a map.

    Digested version is: I’d like to click a button and capture the device orientation at time (T)

    SKnox

    #341940 Reply

    Brandon
    Member

    Try something like this:

    // Wait for device API libraries to load
    //
    document.addEventListener(“deviceready”, onDeviceReady, false);

    // device APIs are available
    //
    function myTimerFunction() {
    navigator.accelerometer.getCurrentAcceleration(onSuccess, onError);
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
    alert(‘Acceleration X: ‘ + acceleration.x + ‘\n’ +
    ‘Acceleration Y: ‘ + acceleration.y + ‘\n’ +
    ‘Acceleration Z: ‘ + acceleration.z + ‘\n’ +
    ‘Timestamp: ‘ + acceleration.timestamp + ‘\n’);
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
    alert(‘onError!’);
    }

    and use the myTimerFunction() in the button

    #341949 Reply

    SKnoxmn
    Member

    Nope, doesn’t return anything. I’ll go check around on the navigator.accelerometer. and see what I come up with.

    SKnox

    #341955 Reply

    SKnoxmn
    Member

    New direction;

    Since I’m wanting to “capture” the angle to be used in some math. How about I just round the angle to the nearest integer. then my onClick would grab whatever is in that field at the time that the button is clicked and just use that.

    I was concerned about how “shakey” the angle was being 10 decimal points deep and changing constantly. So I figure set some level of accuracy and just go with it. Since I’m pretty sure a full degree isn’t going to be a huge issue for my needs.

    So now its on to learning some javascript math against a variable.

    SKnox

    #341958 Reply

    Brandon
    Member

    Round different numbers to the nearest integer:

    var a=Math.round(2.60);
    var b=Math.round(2.50);
    var c=Math.round(2.49);
    var d=Math.round(-2.60);
    var e=Math.round(-2.50);
    var f=Math.round(-2.49);
    The result of a,b,c,d,e, and f will be:

    3
    3
    2
    -3
    -2
    -2

    #341965 Reply

    SKnoxmn
    Member

    Yeah I got that part now. I was wondering for all of about 5 seconds about how to enter it into the JS.

    So I took a shot and turns out it was right. So it would now appear that I can get the math. working for the variables.

    I am looking for the “how to” rather than post the results to a textField Id rather have it just post (or display) as text next to the text label on the screen.

    I was checking out the compass script that Wayne posted but I’ll need to get more coffee in me before it makes sense.

    one more puzzle piece solved,
    SKnox

Viewing 12 posts - 1 through 12 (of 12 total)
Reply To: I need a Break, or a "Pause" or a "Hold"….

You must be logged in to post in the forum log in