在javascript代码

时间:2015-06-08 08:13:08

标签: javascript tizen

我想在javascript代码中添加一个按钮,它使用Tizen sdk存储来自Gear 2的心率。在我的代码中,主div插入到javascript代码中。我使用以下代码:

//HTML CODE:
<div id="chartContainer" class="chart"></div>

//JAVASCRIPT CODE:
var chart = new CanvasJS.Chart("chartContainer",{
    title :{
        fontColor: "#ccc",
        text: "Heart Rate"
    },
    backgroundColor: "#222",
    data: [{
        color: "#CD5C5C",
        type: "line",
        dataPoints: dps 
    }]
});
var lastSecond = -1;
var updateChart = function (heartrate) {
    time = new Date().getTime() - initial;
    console.log("[" + time + ", " + heartrate + "]");
    temp = heartrate;
    console.log("tempVar"+ temp);

     tizen.filesystem.resolve(
             'documents',
             function(dir){
               documentsDir = dir; dir.listFiles(onsuccess,onerror);
             }, function(e) {
               console.log("Error" + e.message);
             }, "a"
         );

    dps.push({
        x: time / 1000.0,
        y: heartrate
    });
    if (dps.length > dataLength)
    {
        dps.shift();                
    }
    var second = Math.round(time / 1000.0);
    console.log(history.length);
    if(lastSecond != second) {
        // TODO use avg heart rate instead of smapshot.
        history.push({
            x: second,
            y: heartrate
        });
        if(history.length > historyDataLength) {
            history.shift();
        }
        lastSecond = second;
    }

    if(dps.length >= dataLength) {
        chart.render();
    }
    var hrchart = "<center>" + heartrate + "bps</center><table width='100%' cellpadding=4px>";
    for(var i = history.length - historyDataLength; i >= 0 && i < history.length; i++) {
        hrchart += "<tr><td align='right' width='50%'>" + history[i].x + "s</td><td width='50%'>" + history[i].y + "bps</td></tr>";
    }
    hrchart += "</table>";
    $('#textbox').html(hrchart);
};

updateChart(0);
updateChart(250);
for(var i = 0; i < dataLength; i++) {
    updateChart(0);
}

我想创建一个两个按钮,一个用于在点击时关闭应用程序,另一个用于在点击时存储数据。如何在javascript代码中添加这两个按钮?其次是熟悉“tizenhwkey”键的人吗?究竟是哪个关键?第三,我使用以下命令window.webapis.motion.start("HRM", onchangedCB)打开心率传感器。如何关闭心率传感器?函数onchangeCB如下:

function onchangedCB(hrmInfo) 
{
   if(hrmInfo.heartRate > 0) {


            // add eventListener for tizenhwkey
            document.addEventListener('tizenhwkey', function(e) {
                if(e.keyName == "back")

                    tizen.application.getCurrentApplication().exit();
            });

       updateChart(hrmInfo.heartRate);
   } else {
       $('#textbox').html("No heart rate detected.");
   }
}

这假设在按下后退按钮时关闭应用程序。然而,齿轮2只有一个按钮。这个按钮是tiwzenhwkey吗?对于hrm,我使用以下code。对于我正在使用的文件:

function onsuccess(files) {
       var testFile = null;
       try{
          testFile = documentsDir.createFile("test.txt");
       if (testFile !== null) {
         testFile.openStream(
             "a",
             function(fs){
               fs.write(temp+"\n\n\n");
               fs.close();
             }, function(e){
               console.log("Error " + e.message);
             }, "UTF-8"
         );
       }
       }
       catch (e) { // file already exist -> append content
           testFile = documentsDir.resolve('test.txt');
            if(testFile !== null)
            {
                testFile.openStream(
                     "a",
                     function(fs){
                       fs.write(temp+"\n\n\n");
                       fs.close();
                     }, function(e){
                       console.log("Error " + e.message);
                     }, "UTF-8"
                 );
            }
        }
     }
     function onerror(error) {
       console.log("The error " + error.message + " occurred when listing the files in the selected folder");
     }

temp = heartrate;   
    tizen.filesystem.resolve(
            'documents',
             function(dir){
             documentsDir = dir; 
             dir.listFiles(onsuccess,onerror);
             }, function(e) {
             console.log("Error" + e.message);
             }, "a"
    );

2 个答案:

答案 0 :(得分:2)

<强> 1。在JS代码中创建按钮

var b1 = document.createElement("BUTTON"); // Create Button
var b2 = document.createElement("BUTTON");

// Assign text to your button
b1.textContent = "Start";
b2.textContent = "Exit";

// Register click handlers to call respective functions
b1.onclick = function() {/*Code here*/};
b2.onclick = function() {/*Code here*/};

// Append them in your DOM i.e to show it on your page.
// Suppose to append it to an existing element in your page with id as "appp".
var attachTo = document.getElementById("appp");
attachTo.appendChild(b1);
attachTo.appendChild(b2);

<强> 2。 TIZENHWKEY

On Gear,&#39; tizenhwkey &#39;意味着&#34; 向下滑动&#34;手势和&#34; 向上滑动&#34;手势。

向下滑动充当后退键,类似于在手机上的工作方式。 向上滑动充当菜单按钮,类似于在手机上的工作方式

您可以使用以下代码处理我上面提到的两种手势。

document.addEventListener('tizenhwkey', function(e)  {
            if(e.keyName == "menu") {
            }

            if(e.keyName == "back") {
                // you need to write exit statement
                tizen.application.getCurrentApplication().exit();
            }
    }
  1. 关于HRM停止 - 使用此功能停止监控HRM。

    webapis.motion.stop(&#34; HRM&#34);

  2. 新行问题 - 尝试此工作。

    fs.write(temp +&#34; \ n \ n \ n&n&#34;);

答案 1 :(得分:-4)

我还是初学者,但我知道如何打开窗户并关闭它的基础知识。也许这可以帮到你。

 <button onclick="openWin()">Open "myWindow"</button>
 <button onclick="closeWin()">Close "myWindow"</button>

<script>
var myWindow;
function openWin() { 
myWindow = window.open("", "myWindow", "width=200, height=100"); 
myWindow.document.write("<p>This is 'myWindow'</p>");
}

function closeWin(){ 
myWindow.close();
}