1
+ /*
2
+ * Scala.js (https://www.scala-js.org/)
3
+ *
4
+ * Copyright EPFL.
5
+ *
6
+ * Licensed under Apache License 2.0
7
+ * (https://www.apache.org/licenses/LICENSE-2.0).
8
+ *
9
+ * See the NOTICE file distributed with this work for
10
+ * additional information regarding copyright ownership.
11
+ */
12
+
13
+ package scala .scalajs .js
14
+
15
+ import scala .concurrent .duration .FiniteDuration
16
+
17
+ /**
18
+ * <span class="badge badge-non-std" style="float: right;">Non-Standard</span>
19
+ * Non-standard, but in general well supported methods to schedule asynchronous
20
+ * execution.
21
+ *
22
+ * The methods in this package work in all JavaScript virtual machines
23
+ * supporting `setTimeout` and `setInterval`.
24
+ */
25
+ package object timers {
26
+
27
+ /** Schedule something for execution in `interval` milliseconds.
28
+ *
29
+ * @param interval duration in milliseconds to wait
30
+ * @param body code to execute after `interval` has passed
31
+ * @return A handle that can be used to cancel the timeout by passing it
32
+ * to [[clearTimeout ]].
33
+ * @note Uses JavaScript's non-standard `setTimeout`
34
+ */
35
+ def setTimeout (interval : Double )(body : => Unit ): SetTimeoutHandle =
36
+ RawTimers .setTimeout(() => body, interval)
37
+
38
+ /** Schedule something for execution after a duration.
39
+ *
40
+ * @param interval duration to wait
41
+ * @param body code to execute after `interval` has passed
42
+ * @return A handle that can be used to cancel the timeout by passing it
43
+ * to [[clearTimeout ]].
44
+ * @note Uses JavaScript's non-standard `setTimeout`
45
+ */
46
+ def setTimeout (interval : FiniteDuration )(body : => Unit ): SetTimeoutHandle =
47
+ RawTimers .setTimeout(() => body, interval.toMillis.toDouble)
48
+
49
+ /** Cancel a timeout execution
50
+ * @param handle The handle returned by
51
+ * [[setTimeout(interval:scala\.concurrent\.duration\.FiniteDuration)* setTimeout ]].
52
+ * @note Uses JavaScript's non-standard `clearTimeout`
53
+ */
54
+ def clearTimeout (handle : SetTimeoutHandle ): Unit =
55
+ RawTimers .clearTimeout(handle)
56
+
57
+ /** Schedule something for repeated execution every `interval` milliseconds.
58
+ *
59
+ * @param interval duration in milliseconds between executions
60
+ * @param body code to execute after each `interval`
61
+ * @return A handle that can be used to cancel the interval by passing it
62
+ * to [[clearInterval ]].
63
+ * @note Uses JavaScript's non-standard `setInterval`
64
+ */
65
+ def setInterval (interval : Double )(body : => Unit ): SetIntervalHandle =
66
+ RawTimers .setInterval(() => body, interval)
67
+
68
+ /** Schedule something for repeated execution every duration.
69
+ *
70
+ * @param interval duration between executions
71
+ * @param body code to execute after each `interval`
72
+ * @return A handle that can be used to cancel the interval by passing it
73
+ * to [[clearInterval ]].
74
+ * @note Uses JavaScript's non-standard `setInterval`
75
+ */
76
+ def setInterval (interval : FiniteDuration )(body : => Unit ): SetIntervalHandle =
77
+ RawTimers .setInterval(() => body, interval.toMillis.toDouble)
78
+
79
+ /** Cancel an interval execution
80
+ * @param handle The handle returned by
81
+ * [[setInterval(interval:scala\.concurrent\.duration\.FiniteDuration)* setInterval ]].
82
+ * @note Uses JavaScript's non-standard `clearInterval`
83
+ */
84
+ def clearInterval (handle : SetIntervalHandle ): Unit =
85
+ RawTimers .clearInterval(handle)
86
+
87
+ }
0 commit comments