Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Variables In JavaScript

so I currently have a JavaScript code that I run on my iPhone to pull its Accelerometer data. What I would like to do is Pass this data to my Mac Book Pro and use the variables in A python Script. The Variables are changing multiple times a second so it would need to repeatedly send the Variables. I would greatly appreciate any help I can get. Thank you.

<html>
<body>
<div id="content">
    <h1>Accelerometer JavaScript Test</h1>
<ul>
    <li>acceleration x: <span id="accelerationX"></span></li>
    <li>acceleration y: <span id="accelerationY"></span></li>
    <li>Motor Speed: <span id="speed"></span></li>
</ul>
</div>
<script type="text/javascript">
window.ondevicemotion = function(e){
        var x = e.accelerationIncludingGravity.x;
        var y = e.accelerationIncludingGravity.y;
        var newx = x * 100
        var newy = y * 100
        var finalx = Math.round(x);
        var finaly = Math.round(y);
        document.getElementById("accelerationX").innerHTML = finalx
        document.getElementById("accelerationY").innerHTML = finaly
        speed = finalx * 10
        document.getElementById("speed").innerHTML = speed
    }
</script>
</body>
</html>
like image 929
user3078477 Avatar asked Jul 26 '26 14:07

user3078477


1 Answers

You just need to do an ajax call. The first answer of this question may help you.

The answer says:

All you need is to make an ajax request to your pythoncode. You can do this with jquery http://api.jquery.com/jQuery.ajax/, or use just javascript

$.ajax({
  type: "POST",
  url: "~/pythoncode.py",
  data: { param: text}
}).done(function( o ) {
   // do something
});
like image 132
Winner Crespo Avatar answered Jul 28 '26 04:07

Winner Crespo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!