@@ -6,17 +6,19 @@ use wasm_bindgen::{JsCast, JsValue};
6
6
7
7
#[ wasm_bindgen]
8
8
extern "C" {
9
- #[ wasm_bindgen( js_name = "setTimeout" , catch) ]
10
- fn set_timeout ( handler : & Function , timeout : i32 ) -> Result < i32 , JsValue > ;
9
+ type GlobalThis ;
11
10
12
- #[ wasm_bindgen( js_name = "setInterval " , catch) ]
13
- fn set_interval ( handler : & Function , timeout : i32 ) -> Result < i32 , JsValue > ;
11
+ #[ wasm_bindgen( method , js_name = "setTimeout " , catch) ]
12
+ fn set_timeout ( this : & GlobalThis , handler : & Function , timeout : i32 ) -> Result < i32 , JsValue > ;
14
13
15
- #[ wasm_bindgen( js_name = "clearTimeout" ) ]
16
- fn clear_timeout ( handle : i32 ) ;
14
+ #[ wasm_bindgen( method , js_name = "setInterval" , catch ) ]
15
+ fn set_interval ( this : & GlobalThis , handler : & Function , timeout : i32 ) -> Result < i32 , JsValue > ;
17
16
18
- #[ wasm_bindgen( js_name = "clearInterval" ) ]
19
- fn clear_interval ( handle : i32 ) ;
17
+ #[ wasm_bindgen( method, js_name = "clearTimeout" ) ]
18
+ fn clear_timeout ( this : & GlobalThis , handle : i32 ) ;
19
+
20
+ #[ wasm_bindgen( method, js_name = "clearInterval" ) ]
21
+ fn clear_interval ( this : & GlobalThis , handle : i32 ) ;
20
22
}
21
23
22
24
/// A scheduled timeout.
@@ -37,7 +39,8 @@ impl Drop for Timeout {
37
39
/// `clearTimeout` directly.
38
40
fn drop ( & mut self ) {
39
41
if let Some ( id) = self . id {
40
- clear_timeout ( id) ;
42
+ let global = js_sys:: global ( ) . unchecked_into :: < GlobalThis > ( ) ;
43
+ global. clear_timeout ( id) ;
41
44
}
42
45
}
43
46
}
@@ -61,7 +64,8 @@ impl Timeout {
61
64
{
62
65
let closure = Closure :: once ( callback) ;
63
66
64
- let id = set_timeout (
67
+ let global = js_sys:: global ( ) . unchecked_into :: < GlobalThis > ( ) ;
68
+ let id = global. set_timeout (
65
69
closure. as_ref ( ) . unchecked_ref :: < js_sys:: Function > ( ) ,
66
70
millis as i32 ,
67
71
)
@@ -139,7 +143,8 @@ impl Drop for Interval {
139
143
/// `clearInterval` directly.
140
144
fn drop ( & mut self ) {
141
145
if let Some ( id) = self . id {
142
- clear_interval ( id) ;
146
+ let global = js_sys:: global ( ) . unchecked_into :: < GlobalThis > ( ) ;
147
+ global. clear_interval ( id) ;
143
148
}
144
149
}
145
150
}
@@ -162,7 +167,8 @@ impl Interval {
162
167
{
163
168
let closure = Closure :: wrap ( Box :: new ( callback) as Box < dyn FnMut ( ) > ) ;
164
169
165
- let id = set_interval (
170
+ let global = js_sys:: global ( ) . unchecked_into :: < GlobalThis > ( ) ;
171
+ let id = global. set_interval (
166
172
closure. as_ref ( ) . unchecked_ref :: < js_sys:: Function > ( ) ,
167
173
millis as i32 ,
168
174
)
0 commit comments