Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit ff79977

Browse files
author
Søren Ernst
authored
Update README.md
1 parent e89dd1b commit ff79977

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

README.md

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,70 @@ var timer = moment.duration(5, "seconds").timer(function() {
3636

3737
#### In this example we will create an interval like timer. Simply set the <b>loop</b> attribute.
3838
```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() {
4042
// Callback
4143
});
4244
```
4345

4446
#### Prevent the timer from starting on creation, by using the <b>start</b> attribute, so we can start it later.
4547
```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() {
4752
// Callback
4853
});
4954
timer.start();
5055
```
5156

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.
5358
```javascript
5459
var timer = moment.duration(5, "seconds").timer({
55-
wait: moment.duration(1, "hour"),
56-
loop: true,
60+
loop: true,
61+
start: true
5762
}, function() {
5863
// Callback
5964
});
65+
timer.stop();
66+
timer.start();
6067
```
6168

62-
#### Getting the remaining duration of a timer. (How long until it ends or loops again)
69+
#### See if a timer has been stopped.
6370
```javascript
6471
var timer = moment.duration(5, "seconds").timer({
6572
loop: true,
73+
start: true
6674
}, function() {
6775
// Callback
6876
});
77+
timer.stop();
78+
timer.isStopped(); // True
6979

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+
});
71103
```
72104

73105
#### Setting the duration of a timer. This will override the duration set when the timer was created.
@@ -83,7 +115,29 @@ timer.duration("2", "seconds");
83115
timer.duration({seconds: 2});
84116
```
85117

86-
#### 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.
87141
```javascript
88142
var timer = moment.duration().timer({
89143
}, function() {

0 commit comments

Comments
 (0)