使用phonegap时,Javascript全局变量不起作用

时间:2014-07-04 10:11:37

标签: javascript android html5 cordova

我使用phonegap创建了加速器android应用程序。在应用程序中显示加速器 值并将该值保存到csv文件

但是在csv文件中获取null值。按照我显示用于创建此应用程序的链接。

http://docs.phonegap.com/en/1.2.0/phonegap_accelerometer_accelerometer.md.html

http://docs.phonegap.com/en/1.2.0/phonegap_file_file.md.html#FileWriter

在我的代码'mycsv'变量中没有得到'gotFileWriter'函数的值

<!DOCTYPE html>
<html>
  <head>
    <title>Acceleration Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>


    <script type="text/javascript" charset="utf-8">

    // The watch id references the current `watchAcceleration`
    var watchID = null;

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

    // Cordova is ready
    //
    function onDeviceReady() {
        startWatch();
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }

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

        // Update acceleration every 1 milli seconds
        var options = { frequency: 1 };

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

    }

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

    // onSuccess: Get a snapshot of the current acceleration
    //
     var mycsv;

    function onSuccess(acceleration) {

        var d = new Date();
        document.getElementById('accelerometer').innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
                            'Acceleration Y: ' + acceleration.y + '<br />' +
                            'Acceleration Z: ' + acceleration.z + '<br />' + 
                            'Timestamp: '      + acceleration.timestamp + '<br />'+


                            'Hour: ' + d.getHours() + '<br />' + 
                            'Minit: ' + d.getMinutes() + '<br />' + 
                            'second: ' + d.getSeconds() + '<br />' + 
                            'millisecond: ' + d.getMilliseconds() + '<br />' 



                            ;

     var str2 = d.getFullYear().toString();
     var str1 = ",";

       mycsv= str2.concat(str1,d.getMonth().toString(),str1,d.getDate().toString(),str1,d.getHours().toString(),str1,d.getMinutes().toString(),str1,d.getSeconds().toString(),str1,d.getMilliseconds().toString());

        document.getElementById('csvitem').innerHTML = mycsv;




    }

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

    // csv file create and save /////////////////////////

     function gotFS(fileSystem) {
        fileSystem.root.getFile("kinectAcceler.csv", {create: true, exclusive: false}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }

    function gotFileWriter(writer) {
          writer.onwriteend = function(evt) {
                console.log('finished writing');
                if (callback !== undefined) {
                    callback(writer);
                }
            };
            writer.write(window.mycsv);


    }

    function fail(error) {
        console.log(error.code);
    }





    </script>
  </head>
  <body>
    <div id="accelerometer">Waiting for accelerometer...</div>
    <button onclick="stopWatch();">Stop Watching</button>
    <p id="csvitem">..<p>


  </body>
</html>

0 个答案:

没有答案