-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 935 Bytes
/
index.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
module.exports = swanStation
function swanStation (options) {
if (!options.call && typeof options.call !== 'function') throw new Error('function to call must be set')
var pressed = Date.now()
var time = options.time || 1000 * 60 * 108
var destroyed = false
var check = function () {
if (!destroyed) {
var now = Date.now()
if (now > pressed + time) {
if (options.reset) {
pressed = pressed + time
var checkDelay = now - pressed
setTimeout(check, time - checkDelay)
} else {
destroyed = true
}
options.call()
} else {
setTimeout(check, time - (now - pressed))
}
}
}
setTimeout(check, time)
var button = function () {
pressed = Date.now()
return pressed
}
button.destroy = function () {
destroyed = true
}
button.isDestroyed = function () {
return destroyed
}
return button
}