You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 4, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+62-8Lines changed: 62 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,38 +36,70 @@ var timer = moment.duration(5, "seconds").timer(function() {
36
36
37
37
#### In this example we will create an interval like timer. Simply set the <b>loop</b> attribute.
38
38
```javascript
39
-
var timer =moment.duration(5, "seconds").timer({loop:true}, function() {
39
+
var timer =moment.duration(5, "seconds").timer({
40
+
loop:true
41
+
}, function() {
40
42
// Callback
41
43
});
42
44
```
43
45
44
46
#### Prevent the timer from starting on creation, by using the <b>start</b> attribute, so we can start it later.
45
47
```javascript
46
-
var timer =moment.duration(5, "seconds").timer({loop:true, start:false}, function() {
48
+
var timer =moment.duration(5, "seconds").timer({
49
+
loop:true,
50
+
start:false
51
+
}, function() {
47
52
// Callback
48
53
});
49
54
timer.start();
50
55
```
51
56
52
-
#### Delaying a timer can be done by using the <b>wait</b> attribute.
57
+
#### Stopping a timer can be done by using the stop() function. After the timer has been stopped, the start function can be used to start it again.
53
58
```javascript
54
59
var timer =moment.duration(5, "seconds").timer({
55
-
wait:moment.duration(1, "hour"),
56
-
loop:true,
60
+
loop:true,
61
+
start:true
57
62
}, function() {
58
63
// Callback
59
64
});
65
+
timer.stop();
66
+
timer.start();
60
67
```
61
68
62
-
#### Getting the remaining duration of a timer. (How long until it ends or loops again)
69
+
#### See if a timer has been stopped.
63
70
```javascript
64
71
var timer =moment.duration(5, "seconds").timer({
65
72
loop:true,
73
+
start:true
66
74
}, function() {
67
75
// Callback
68
76
});
77
+
timer.stop();
78
+
timer.isStopped(); // True
69
79
70
-
var remainingDuration =timer.getRemainingDuration();
80
+
timer.start();
81
+
timer.isStopped(); // False
82
+
```
83
+
84
+
#### Delaying a timer can be done by using the <b>wait</b> attribute. In the example below, the timer will wait for an hour and five seconds before it executes.
85
+
```javascript
86
+
var timer =moment.duration(5, "seconds").timer({
87
+
wait:moment.duration(1, "hour"),
88
+
loop:true,
89
+
}, function() {
90
+
// Callback
91
+
});
92
+
```
93
+
94
+
#### Having the timer execute after waiting, can be done by using the <b>executeAfterWait</b> attribute. In the example below, the timer will wait for an hour, then execute and do so again after another give seconds.
95
+
```javascript
96
+
var timer =moment.duration(5, "seconds").timer({
97
+
wait:moment.duration(1, "hour"),
98
+
executeAfterWait:true,
99
+
loop:true,
100
+
}, function() {
101
+
// Callback
102
+
});
71
103
```
72
104
73
105
#### Setting the duration of a timer. This will override the duration set when the timer was created.
#### In this example a timer is set to start instantly.<br>Since the loop attribute hasn't been set, it will behave like a timeout, however it is possible to reuse it simply by calling start().
118
+
#### Getting the duration of a timer.
119
+
```javascript
120
+
var timer =moment.duration(5, "seconds").timer({
121
+
loop:true,
122
+
}, function() {
123
+
// Callback
124
+
});
125
+
126
+
var duration =timer.getDuration();
127
+
```
128
+
129
+
#### Getting the remaining duration of a timer. (How long until it ends or loops again)
130
+
```javascript
131
+
var timer =moment.duration(5, "seconds").timer({
132
+
loop:true,
133
+
}, function() {
134
+
// Callback
135
+
});
136
+
137
+
var remainingDuration =timer.getRemainingDuration();
138
+
```
139
+
140
+
#### In this example we an see that a "timeout like timer" can be reused. If you run this example, you will notice it executing the callback twice. This is to show that even if you use the timer like a timeout, it can be reused, unlike JavaScripts native setTimeout that will only function once.
0 commit comments