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
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use std::time::Duration;
use js_sys::Date;
#[derive(Debug)]
pub struct Timer {
duration: f64,
last: f64,
}
impl Timer {
pub fn new(duration: Duration) -> Self {
Timer {
last: Date::now(),
duration: duration.as_millis() as f64,
}
}
pub fn reset(&mut self) {
self.last = Date::now();
}
pub fn ringing(&self) -> bool {
(Date::now() - self.last) > self.duration
}
pub fn ring_manual(&mut self) {
self.last -= self.duration;
}
}
}
else {
use std::time::{Duration, Instant};
#[derive(Debug)]
pub struct Timer {
duration: Duration,
last: Instant,
}
impl Timer {
pub fn new(duration: Duration) -> Self {
Timer {
last: Instant::now(),
duration,
}
}
pub fn reset(&mut self) {
self.last = Instant::now();
}
pub fn ringing(&self) -> bool {
Instant::now().saturating_duration_since(self.last) > self.duration
}
pub fn ring_manual(&mut self) {
self.last -= self.duration;
}
}
}
}