|
| 1 | +/*** |
| 2 | +Bindings to functions available in the global JavaScript scope. |
| 3 | +*/ |
| 4 | + |
| 5 | +/** |
| 6 | +An `id` representing a timeout started via `setTimeout`. |
| 7 | +
|
| 8 | +See [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) on MDN. |
| 9 | +*/ |
| 10 | +type timeoutId = Js.Global.timeoutId |
| 11 | + |
| 12 | +/** |
| 13 | +`setTimeout(callback, durationInMilliseconds)` starts a timer that will execute `callback` after `durationInMilliseconds`. |
| 14 | +
|
| 15 | +See [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) on MDN. |
| 16 | +
|
| 17 | +## Examples |
| 18 | +```rescript |
| 19 | +// Log to the console after 2 seconds (2000 milliseconds). |
| 20 | +let timeoutId = setTimeout(() => { |
| 21 | + Console.log("This prints in 2 seconds.") |
| 22 | +}, 2000) |
| 23 | +``` |
| 24 | +*/ |
| 25 | +@val |
| 26 | +external setTimeout: (unit => unit, int) => timeoutId = "setTimeout" |
| 27 | + |
| 28 | +/** |
| 29 | +`setTimeoutFloat(callback, durationInMilliseconds)` starts a timer that will execute `callback` after `durationInMilliseconds`. |
| 30 | +
|
| 31 | +The same as `setTimeout`, but allows you to pass a `float` instead of an `int` for the duration. |
| 32 | +
|
| 33 | +See [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) on MDN. |
| 34 | +
|
| 35 | +## Examples |
| 36 | +```rescript |
| 37 | +// Log to the console after 2 seconds (2000 milliseconds). |
| 38 | +let timeoutId = setTimeoutFloat(() => { |
| 39 | + Console.log("This prints in 2 seconds.") |
| 40 | +}, 2000.) |
| 41 | +``` |
| 42 | +*/ |
| 43 | +@val |
| 44 | +external setTimeoutFloat: (unit => unit, float) => timeoutId = "setTimeout" |
| 45 | + |
| 46 | +/** |
| 47 | +`clearTimeout(timeoutId)` clears a scheduled timeout if it hasn't already executed. |
| 48 | +
|
| 49 | +See [`clearTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) on MDN. |
| 50 | +
|
| 51 | +## Examples |
| 52 | +```rescript |
| 53 | +let timeoutId = setTimeout(() => { |
| 54 | + Console.log("This prints in 2 seconds.") |
| 55 | +}, 2000) |
| 56 | +
|
| 57 | +// Clearing the timeout right away, before 2 seconds has passed, means that the above callback logging to the console will never run. |
| 58 | +clearTimeout(timeoutId) |
| 59 | +``` |
| 60 | +*/ |
| 61 | +@val |
| 62 | +external clearTimeout: timeoutId => unit = "clearTimeout" |
| 63 | + |
| 64 | +/** |
| 65 | +An `id` representing an interval started via `setInterval`. |
| 66 | +
|
| 67 | +See [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) on MDN. |
| 68 | +*/ |
| 69 | +type intervalId = Js.Global.intervalId |
| 70 | + |
| 71 | +/** |
| 72 | +`setInterval(callback, intervalInMilliseconds)` starts an interval that will execute `callback` every `durationInMilliseconds` milliseconds. |
| 73 | +
|
| 74 | +See [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) on MDN. |
| 75 | +
|
| 76 | +## Examples |
| 77 | +```rescript |
| 78 | +// Log to the console ever 2 seconds (2000 milliseconds). |
| 79 | +let intervalId = setInterval(() => { |
| 80 | + Console.log("This prints every 2 seconds.") |
| 81 | +}, 2000) |
| 82 | +``` |
| 83 | +*/ |
| 84 | +@val |
| 85 | +external setInterval: (unit => unit, int) => intervalId = "setInterval" |
| 86 | + |
| 87 | +/** |
| 88 | +`setIntervalFloat(callback, intervalInMilliseconds)` starts an interval that will execute `callback` every `durationInMilliseconds` milliseconds. |
| 89 | +
|
| 90 | +The same as `setInterval`, but allows you to pass a `float` instead of an `int` for the duration. |
| 91 | +
|
| 92 | +See [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) on MDN. |
| 93 | +
|
| 94 | +## Examples |
| 95 | +```rescript |
| 96 | +// Log to the console ever 2 seconds (2000 milliseconds). |
| 97 | +let intervalId = setIntervalFloat(() => { |
| 98 | + Console.log("This prints every 2 seconds.") |
| 99 | +}, 2000.) |
| 100 | +``` |
| 101 | +*/ |
| 102 | +@val |
| 103 | +external setIntervalFloat: (unit => unit, float) => intervalId = "setInterval" |
| 104 | + |
| 105 | +/** |
| 106 | +`clearInterval(intervalId)` clears a scheduled interval. |
| 107 | +
|
| 108 | +See [`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/clearInterval) on MDN. |
| 109 | +
|
| 110 | +## Examples |
| 111 | +```rescript |
| 112 | +let intervalId = setInterval(() => { |
| 113 | + Console.log("This prints in 2 seconds.") |
| 114 | +}, 2000) |
| 115 | +
|
| 116 | +// Stop the interval after 10 seconds |
| 117 | +let timeoutId = setTimeout(() => { |
| 118 | + clearInterval(intervalId) |
| 119 | +}, 10000) |
| 120 | +``` |
| 121 | +*/ |
| 122 | +@val |
| 123 | +external clearInterval: intervalId => unit = "clearInterval" |
| 124 | + |
| 125 | +/** |
| 126 | +Encodes a URI by replacing characters in the provided string that aren't valid in a URL. |
| 127 | +
|
| 128 | +This is intended to operate on full URIs, so it encodes fewer characters than what `encodeURIComponent` does. |
| 129 | +If you're looking to encode just parts of a URI, like a query parameter, prefer `encodeURIComponent`. |
| 130 | +
|
| 131 | +See [`encodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) on MDN. |
| 132 | +
|
| 133 | +## Examples |
| 134 | +```rescript |
| 135 | +Console.log(encodeURI("https://rescript-lang.org?array=[someValue]")) |
| 136 | +// Logs "https://rescript-lang.org?array=%5BsomeValue%5D" to the console. |
| 137 | +``` |
| 138 | +
|
| 139 | +*/ |
| 140 | +@val |
| 141 | +external encodeURI: string => string = "encodeURI" |
| 142 | + |
| 143 | +/** |
| 144 | +Decodes a previously encoded URI back to a regular string. |
| 145 | +
|
| 146 | +This is intended to operate on full URIs, so it decodes fewer characters than what `decodeURIComponent` does. |
| 147 | +If you're looking to decode just parts of a URI, like a query parameter, prefer `decodeURIComponent`. |
| 148 | +
|
| 149 | +See [`decodeURI`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI) on MDN. |
| 150 | +
|
| 151 | +## Examples |
| 152 | +```rescript |
| 153 | +Console.log(decodeURI("https://rescript-lang.org?array=%5BsomeValue%5D")) |
| 154 | +// Logs "https://rescript-lang.org?array=[someValue]" to the console. |
| 155 | +``` |
| 156 | +*/ |
| 157 | +@val |
| 158 | +external decodeURI: string => string = "decodeURI" |
| 159 | + |
| 160 | +/** |
| 161 | +Encodes a string so it can be used as part of a URI. |
| 162 | +
|
| 163 | +See [`encodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) on MDN. |
| 164 | +
|
| 165 | +## Examples |
| 166 | +```rescript |
| 167 | +Console.log(encodeURIComponent("array=[someValue]")) |
| 168 | +// Logs "array%3D%5BsomeValue%5D" to the console. |
| 169 | +``` |
| 170 | +*/ |
| 171 | +@val |
| 172 | +external encodeURIComponent: string => string = "encodeURIComponent" |
| 173 | + |
| 174 | +/** |
| 175 | +Decodes a previously URI encoded string back to its original form. |
| 176 | +
|
| 177 | +See [`decodeURIComponent`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent) on MDN. |
| 178 | +
|
| 179 | +## Examples |
| 180 | +```rescript |
| 181 | +Console.log(decodeURIComponent("array%3D%5BsomeValue%5D")) |
| 182 | +// Logs "array=[someValue]" to the console. |
| 183 | +``` |
| 184 | +*/ |
| 185 | +@val |
| 186 | +external decodeURIComponent: string => string = "decodeURIComponent" |
0 commit comments