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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use Closure;
use ;
use window;
use Rc;
/// Javascript [`setTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) function
///
/// This function sets a timer which executes a function or specified piece of code once the timer expires.
///
/// It returns the ID of this timer which can be used with [`clearTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) to cancel the timer
///
///
/// # Arguments
///
/// * `handler` - The Rust closure to execute
///
/// * `timeout` - Tumber of milliseconds to wait before executing the code in `handler`.
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```
/// use webru::{set_timeout};
/// use weblog::console_log;
///
/// set_timeout(
/// // This closure will execute after 4 seconds
/// || {
/// console_log!("Hello world");
/// },
/// 4000,
/// )
/// .unwrap();
///
/// let author = "Shanto".to_string();
///
/// set_timeout(
/// // This closure will execute after 5 seconds
/// move || {
/// if author == "Shanto".to_string() {
/// console_log!("Welcome Shanto");
/// } else {
/// console_log!("We don't know you")
/// }
/// },
/// 5000,
/// )
/// .unwrap();
/// ```
///
/// Javascript [`clearTimeout()`](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) function
///
/// This function cancels a timeout previously established by calling javascript's [`setTimeout()`] fucntion.
///
/// # Arguments
///
/// * `timeout_id` - The identifier of the timeout you want to cancel. This ID was returned by the corresponding call to [`setTimeout()`]
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```
/// use weblog::console_log;
/// use webru::{clear_timeout, set_timeout};
///
/// let timeout_id = set_timeout(
/// // This closure will not execute because we are clearing this timeout before this closure gets called
/// || {
/// console_log!("Hello world");
/// },
/// 4000,
/// )
/// .unwrap();
///
/// // Clearing the timeout
/// clear_timeout(timeout_id)
/// ```
///
/// [`setTimeout()`]: <https://developer.mozilla.org/en-US/docs/Web/API/setTimeout>
/// Javascript [`setInterval()`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) method
///
/// The `setInterval()` method, offered on the [`Window`](https://developer.mozilla.org/en-US/docs/Web/API/Window) and [`Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Worker) interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
///
/// This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling javascript's [`clearInterval()`] function or [`clear_interval`] function
///
///
/// # Arguments
///
/// * `handler` - A Rust closure to be executed every `timeout` milliseconds. The first execution happens after `timeout` milliseconds.
///
/// * `timeout` - The execution interval in milliseconds. 1000 milliseconds == 1 second
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```
/// use std::cell::Cell;
/// use weblog::console_log;
/// use webru::set_interval;
///
/// let counter: Cell<usize> = Cell::new(0);
///
/// set_interval(
/// // This closure will execute after every 2 secends
/// move || {
/// console_log!("The counter: ", counter.get());
/// counter.set(counter.get() + 1);
/// },
/// 2000,
/// )
/// .unwrap();
/// ```
///
/// [`clearInterval()`]: <https://developer.mozilla.org/en-US/docs/Web/API/clearInterval>
///
/// Javascript [`clearInterval()`] function
///
/// The global `clearInterval()` method cancels a timer, repeating action which was previously established by a call to [`setInterval()`]
///
///
/// # Arguments
///
/// * `timeout` - The identifier of the repeated action you want to cancel. This ID was returned by the corresponding call to [`setInterval()`]
///
///
/// # Panics
///
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
///
/// # Example
///
/// ```
/// use std::cell::Cell;
///
/// use weblog::console_log;
/// use webru::{clear_interval, set_interval};
///
/// let counter: Cell<usize> = Cell::new(0);
///
/// let interval_id = set_interval(
/// // This closure will not execute because we are clearing this interval before this closure gets called
/// move || {
/// console_log!("The counter: ", counter.get());
/// counter.set(counter.get() + 1);
/// },
/// 2000,
/// )
/// .unwrap();
///
/// // Clearing the interval
/// clear_interval(interval_id)
/// ```
///
/// [`setInterval()`]: <https://developer.mozilla.org/en-US/docs/Web/API/setInterval>
/// [`clearInterval()`]: <https://developer.mozilla.org/en-US/docs/Web/API/clearInterval>
///
/// Combination of [`set_timeout()`] and [`clear_timeout()`] functions
///
/// # Panics
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// # Example
///
/// ```rust
/// use std::cell::Cell;
/// use std::rc::Rc;
/// use webru::{set_timeout, Timeout};
///
/// #[derive(Clone, Copy, Debug, PartialEq)]
/// enum EarthShape {
/// Flat,
/// Round,
/// }
///
/// let earth_shape = Rc::new(Cell::new(EarthShape::Flat));
///
/// {
/// let earth_shape = Rc::clone(&earth_shape);
///
/// // setTimeout without stopping it
/// // it will run after 4 secs
/// Timeout::start(
/// move || {
/// // mutating the state of the earth_shape
/// earth_shape.set(EarthShape::Round);
/// },
/// 4000, // 4 secs
/// );
/// }
///
/// set_timeout(
/// move || {
/// // After 4 seconds, the earth_shape should be `Round`
/// assert_eq!(earth_shape.get(), EarthShape::Round);
/// },
/// 5000, // 5 secs
/// )
/// .unwrap();
///
/// let earth_shape = Rc::new(Cell::new(EarthShape::Flat));
///
/// {
/// let earth_shape = Rc::clone(&earth_shape);
///
/// // setTimeout by stopping it
/// // it will not run because we are stopping it by calling the `stop` method
/// let timeout: Timeout = Timeout::start(
/// move || {
/// // mutating the state of the earth_shape
/// earth_shape.set(EarthShape::Round);
/// },
/// 4000, // 4 secs
/// );
/// // stop the timeout. This method is equivalent to `clearTimeout` in the browser
/// timeout.stop();
/// }
///
/// set_timeout(
/// move || {
/// // As the timeout is stopped after creating it, the earth_shape should be `Flat`
/// assert_eq!(earth_shape.get(), EarthShape::Flat);
/// },
/// 5000, // 5 secs
/// )
/// .unwrap();
///
/// ```
/// Combination of [`set_interval()`] and [`clear_interval()`] functions
///
/// # Panics
/// This function will panic if you try to call this outside of the web such as `node.js` runtime
///
/// # Example
///
/// ```rust
/// use wasm_bindgen::JsCast;
/// use web_sys::HtmlElement;
/// use weblog::console_log;
/// use webru::{body, callback, create_element, Interval};
///
/// // Start the interval
/// // It will run every 2 seconds until you press the button to stop it.
/// let interval: Interval = Interval::start(
/// || {
/// console_log!("You are in danger :(");
/// },
/// 2000,
/// ); // every 2 seconds
///
/// // onclick event for stopping the interval
/// let onclick = callback({
/// move || {
/// // Stop the interval
/// interval.stop();
/// }
/// });
///
/// // Creating a <button> element for stopping the interval
/// let button: HtmlElement = create_element("button")
/// .dyn_ref::<HtmlElement>()
/// .unwrap()
/// .clone();
///
/// // Set the inner html of the button
/// button.set_inner_html("Stop interval");
/// // Set the onclick event for the button
/// button.set_onclick(Some(&onclick.as_ref().dyn_ref().unwrap()));
///
/// // Now when you press the button the interval should be stop
///
/// onclick.forget();
///
/// // Append the button to the body
/// body().append_child(&button).unwrap();
/// ```