-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathapp.js
75 lines (65 loc) · 1.77 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import TimerState from './config/timerState'
import { CLOUD_ENV_ID } from './config'
App({
onLaunch() {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
traceUser: true,
env: CLOUD_ENV_ID
})
}
this.data = {
timerId: -1,
timerState: TimerState.NONE,
goalId: '',
goalTitle: '',
duration: 0,
beginDate: 0,
pauseDate: 0,
pauseDuration: 0
}
},
startTimer(goalId, goalTitle, onCount) {
const { data } = this
const { timerState, timerId } = data
if (timerState === TimerState.NONE) {
data.goalId = goalId
data.goalTitle = goalTitle
data.beginDate = Date.now()
} else if (timerState === TimerState.PAUSE) {
data.pauseDuration = data.pauseDuration + (Date.now() - data.pauseDate)
data.pauseDate = 0
}
data.timerState = TimerState.ONGOING
if (timerId !== -1) {
clearInterval(timerId)
}
const { beginDate, pauseDuration } = data
data.duration = Date.now() - beginDate - pauseDuration
onCount(data.duration)
const newTimerId = setInterval(() => {
data.duration = Date.now() - beginDate - pauseDuration
onCount(data.duration)
}, 1000)
this.data.timerId = newTimerId
},
pauseTimer() {
this.data.pauseDate = Date.now()
clearInterval(this.data.timerId)
this.data.timerId = -1
this.data.timerState = TimerState.PAUSE
},
stopTimer() {
clearInterval(this.data.timerId)
this.data.timerId = -1
this.data.timerState = TimerState.NONE
this.data.goalId = ''
this.data.goalTitle = ''
this.data.duration = 0
this.data.pauseDuration = 0
this.data.beginDate = 0
this.data.pauseDate = 0
}
})