title: Trying to run a HTML5 accelerometer listener in the background on mobile
body: <p>I'm currently in the process of creating a webapp that detects whether the user is sitting or standing. I have had great success with detecting the tilt of the device, changing a boolean variable and sending the correct data to a graph. </p>
  
  <p>The following code runs perfectly in the background, updating the data every second.</p>
  
  <pre><code>var trackInterval = setInterval(function(){
      if(isSitting){
          addData(myPieChart, "Sitting");
      } else{
          addData(myPieChart, "Standing");
      }
  }, 1000)
  </code></pre>
  
  <p>My issue is that the listening function that changes the variable 'isSitting' does not continue once the browser is closed. This means that the last value of 'isSitting' gets data added to it, even though the device might be tilted otherwise.</p>
  
  <p>This is the code that creates the accelerometer updates:</p>
  
  <pre><code>window.addEventListener("devicemotion", accelerometerUpdate, true);
  
  var isSitting = true;
  
  function accelerometerUpdate(event) {
      var aX = event.accelerationIncludingGravity.x * 100 ;
      var aY = event.accelerationIncludingGravity.y * 100 ;
      var aZ = event.accelerationIncludingGravity.z * 100 ;
  
      if (aY &gt; 600 || aY &lt; -900 ){
          isSitting = false;
      } else{
          isSitting = true;   
      }
  }
  </code></pre>
  
  <p>I have tried Chrome, Opera and Firefox on my Android device. </p>
  
  <p>Any help or tips to work around this would be greatly appreciated</p>