【发布时间】:2015-06-09 15:57:27
【问题描述】:
我想在我拥有的 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 吗?对于人力资源管理,我使用以下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"
);
【问题讨论】:
标签: javascript tizen