(fn, seconds)
| 1042 | } |
| 1043 | |
| 1044 | function timer(fn, seconds) { |
| 1045 | seconds = typeof seconds === 'function' ? seconds() : seconds |
| 1046 | if (!seconds) |
| 1047 | return { cancel: noop, start: noop } |
| 1048 | |
| 1049 | let timer |
| 1050 | return { |
| 1051 | cancel() { |
| 1052 | timer && (clearTimeout(timer), timer = null) |
| 1053 | }, |
| 1054 | start() { |
| 1055 | timer && clearTimeout(timer) |
| 1056 | timer = setTimeout(done, seconds * 1000, arguments) |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | function done(args) { |
| 1061 | fn.apply(null, args) |
| 1062 | timer = null |
| 1063 | } |
| 1064 | } |