在 javascript 代码中添加按钮
Add buttons inside javascript code
我想在我拥有的 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"
);
我还是个初学者,但我知道如何打开 window 和关闭它的基础知识。也许这可以帮到你。
<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();
}
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
在 Gear 上,“tizenhwkey”表示“向下滑动”手势和“向上滑动[=43=” ]”的手势。
向下滑动 用作 返回 键,类似于在 phone 上的工作方式。 向上滑动 用作 菜单 按钮,类似于它在 phone
上的工作方式
您可以使用下面的代码来处理我上面提到的两种手势。
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName == "menu") {
}
if(e.keyName == "back") {
// you need to write exit statement
tizen.application.getCurrentApplication().exit();
}
}
关于 HRM 停止 - 使用它来停止监控 HRM。
webapis.motion.stop("HRM");
新行问题 - 试试这个是否有效。
fs.write(temp + "\n\n\n");
我想在我拥有的 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"
);
我还是个初学者,但我知道如何打开 window 和关闭它的基础知识。也许这可以帮到你。
<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();
}
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
在 Gear 上,“tizenhwkey”表示“向下滑动”手势和“向上滑动[=43=” ]”的手势。
向下滑动 用作 返回 键,类似于在 phone 上的工作方式。 向上滑动 用作 菜单 按钮,类似于它在 phone
上的工作方式您可以使用下面的代码来处理我上面提到的两种手势。
document.addEventListener('tizenhwkey', function(e) {
if(e.keyName == "menu") {
}
if(e.keyName == "back") {
// you need to write exit statement
tizen.application.getCurrentApplication().exit();
}
}
关于 HRM 停止 - 使用它来停止监控 HRM。
webapis.motion.stop("HRM");
新行问题 - 试试这个是否有效。
fs.write(temp + "\n\n\n");