0

我们正在使用 PhoneGap 开发一个使用 iPhone 加速度计的计步器应用程序。

以下是我们当前正在运行的代码的副本:

<!DOCTYPE html>
<html>

<head>
  <title>Accelerometer</title>

  <script type="text/javascript" charset="utf-8" src="js/cordova-1.7.0rc1.js"></script>

  <script type="text/javascript" charset="utf-8">
    // The watch id references the current `watchAcceleration`
    var watchID = null;
    var stepCount = 0;
    window.localStorage.setItem('exp');
    var expGain = 0;
    var totalExp = window.localStorage.getItem('exp');
    var userAge = window.localStorage.getItem('age');
    var handicap = 0;

    if (userAge <= 10) {
      handicap = 10;
    } else if (userAge > 10) {
      handicap = 5;
    }

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //e7
    function onDeviceReady() {
      startWatch();
    }

    // Start watching the acceleration
    //
    function startWatch() {

      // Update acceleration every 5 seconds
      var options = {
        frequency: 1000
      };

      watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    }

    // Stop watching the acceleration
    //
    function stopWatch() {
      if (watchID) {
        navigator.accelerometer.clearWatch(watchID);
        watchID = null;
      }
    }

    //count steps and calculate experience gained
    function countSteps(accelx, timestamp, expGain, totalExp) {
      var element = document.getElementById('accelerometer');
      //var accCount = Math.round(acceleration.x);
      //stepCount = stepCount + Math.abs(accCount);
      stepCount = stepCount + 1;
      expGain = stepCount * handicap;
      totalExp = totalExp + expGain;
      //window.localStorage.setItem('exp', totalExp);       
      element.innerHTML = '<br>Step Count: ' + stepCount + '<br/>' +
        'Acceleration X: ' + Math.abs(accelx) + '<br />' +
        'Timestamp: ' + timestamp + '<br />' +
        'Experience: ' + totalExp + '<br />';
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
      var element = document.getElementById('accelerometer');
      var accelx = Math.round(acceleration.x)
      var timestamp = acceleration.timestamp
      //element.innerHTML = 'Acceleration X: ' + Math.abs(accelx) + '<br />' +
      //'Acceleration Y: ' + acceleration.y + '<br />' +
      //'Acceleration Z: ' + acceleration.z + '<br />' + 
      //'Timestamp: '      + acceleration.timestamp + '<br />';
      if (Math.abs(accelx) > 1) {
        countSteps(accelx, timestamp, expGain, totalExp);
      }
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
      alert('onError!');
    }
  </script>
  <link media="only screen and (max-device-width: 480px)" href="css/iPhone.css" rel="stylesheet" type="text/css">
</head>

<body>
  <div id="accelerometer">Waiting for accelerometer...</div>
  <button onclick="startWatch();">Start Watching</button>
  <button onclick="stopWatch();">Stop Watching</button>
  <br />
  <p><a href="index.html">back</a>
  </p>
</body>

</html>

问题是:我们不断在"undefined"我们的结果旁边获得window.localStorage.setItem('exp');. 我们整天都在努力尝试解决这个问题。

4

2 回答 2

3

setItem接受两个参数(一个键和一个值)。如果不指定参数,undefined则为默认值。

window.localStorage.setItem('exp');方法window.localStorage.setItem('exp', undefined);

如果您希望它具有其他值,则需要指定它。

于 2012-05-17T11:34:19.490 回答
2

localStorage.setItem 不需要第二个参数吗?你有没有尝试过:

window.localStorage.setItem("exp","");
于 2012-05-17T11:33:47.233 回答