【发布时间】:2022-01-04 23:52:26
【问题描述】:
我想捕捉并保存(也可能是console.log()),从我按下具有startTest() 的开始按钮的那一刻起的加速度。但是我不知道该怎么做,因为我从未使用过这些DeviceMotionEvent。任何有关如何开始捕获加速度的帮助,如here 所述,并在我按下停止按钮时停止捕获它,我们将不胜感激。谢谢!
这是我的带有按钮的起始代码:
<template>
<q-page padding>
<div class="text-center text-h5 q-mt-lg">
{{ $t('tasks.motion.title') }}
</div>
<div class="row justify-center q-mt-lg">
<q-btn
@click="startTest"
v-show="!isStarted"
:label="$t('common.start')"
/>
<q-btn
@click="completeTest"
v-show="isStarted"
:label="$t('common.complete')"
/>
</div>
</q-page>
</template>
<script>
import phone from 'modules/phone'
import userinfo from 'modules/userinfo'
import { format as Qformat } from 'quasar'
const TEST_DURATION = 60
export default {
name: 'MotionOrientationPage',
props: {
sKey: String,
tId: Number
},
data: function () {
return {
isSignalCheck: true,
isStarted: false,
isCompleted: false,
timer: undefined,
totalTime: TEST_DURATION,
startedTS: undefined,
completionTS: undefined,
acceleration: [],
distance: 0
}
},
mounted: async function () {
},
methods: {
async startTest () {
this.isStarted = true
this.startedTS = new Date()
this.startTimer()
phone.screen.forbidSleep()
},
startTimer () {
this.totalTime = TEST_DURATION
this.timer = setInterval(() => this.countDown(), 1000)
},
stopTimer () {
clearInterval(this.timer)
},
countDown () {
if (this.totalTime >= 1) {
this.totalTime--
} else {
this.completeTest()
}
},
completeTest () {
this.isStarted = false
this.completionTS = new Date()
this.stopTimer()
phone.screen.allowSleep()
this.isCompleted = true
// package the report
const sKey = this.sKey
const taskId = parseInt(this.taskId)
const userKey = userinfo.user._key
let report = {
userKey: userKey,
sKey: sKey,
taskId: taskId,
createdTS: new Date(),
startedTS: this.startedTS,
completionTS: this.completionTS,
acceleration: this.acceleration,
distance: this.distance
}
this.$router.push({ name: 'reportMotionOrientation', params: { report: report } })
}
},
computed: {
minutes () {
return Qformat.pad(Math.floor(this.totalTime / 60))
},
seconds () {
return Qformat.pad(this.totalTime - (this.minutes * 60))
}
},
beforeDestroy: function () {
this.stopTimer()
phone.screen.allowSleep()
}
}
</script>
【问题讨论】:
标签: javascript vue.js