This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
forked from annsonn/simple-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-timer.html
131 lines (117 loc) · 3.2 KB
/
simple-timer.html
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<link rel="import" href="../polymer/polymer.html">
<!--
`simple-timer`
Simple timer.
@demo demo/index.html
-->
<dom-module id="simple-timer">
<template>
<style>
:host {
display: block;
}
</style>
{{_formattedTime}}
</template>
<script>
Polymer({
is: 'simple-timer',
properties: {
/**
* Start time for the timer in seconds
* @default 60
*/
startTime: {
type: Number,
value: 60
},
/**
* Current time of the timer, in seconds
*/
currentTime: {
type: Number,
notify: true
},
/**
* True if the timer is currently running
* @default false
*/
isRunning: {
type: Boolean,
reflectToAttribute: true,
notify: true,
value: false
},
/**
* Set to true to have timer count up
* @default false
*/
countUp: {
type: Boolean,
value: false
},
/**
* Time the timer has spent running since it was started
*/
_elapsedTime: {
type: Number,
value: 0
},
_formattedTime: {
type: String,
value: '0'
}
},
ready: function() {
if (this.countUp) {
this.set('currentTime', 0);
this.set('_formattedTime', '0');
} else {
this.set('currentTime', this.startTime);
this.set('_formattedTime', this.startTime.toString());
}
},
start: function() {
if ((this.currentTime <= 0 && !this.countUp)
|| (this.currentTime >= this.startTime && this.countUp) ) {
// timer is over
this.currentTime = this.countUp ? this.startTime : 0;
}
if (!this.startTime || this.isRunning) {
this.pause();
return;
}
this._elapsed = performance.now()/1000;
this.isRunning = true;
window.requestAnimationFrame(this._decreaseTimer.bind(this));
},
pause: function() {
this.isRunning = false;
},
_decreaseTimer: function(timestamp) {
if (!this.isRunning) {
return;
}
if ((this.currentTime <= 0 && !this.countUp)
|| (this.currentTime >= this.startTime && this.countUp) ) {
// timer is over
this.currentTime = this.countUp ? this.startTime : 0;
this.pause();
this.fire('simple-timer-end');
return;
}
var now = timestamp/1000;
// Compute the relative progress based on the time spent running
var progress = now - this._elapsed;
this.currentTime = this.countUp ? this.currentTime + progress : this.currentTime - progress;
this._formattedTime = this._formatTime(this.currentTime);
this._elapsed = now;
window.requestAnimationFrame(this._decreaseTimer.bind(this));
},
_formatTime: function(time) {
var timeString = time.toString().split('.');
return timeString[0].indexOf('-') === 0 ? 0 : timeString[0] + '.' + timeString[1].substring(0,2);
}
});
</script>
</dom-module>