@@ -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,11 +64,13 @@ impl Timeout {
61
64
{
62
65
let closure = Closure :: once ( callback) ;
63
66
64
- let id = set_timeout (
65
- closure. as_ref ( ) . unchecked_ref :: < js_sys:: Function > ( ) ,
66
- millis as i32 ,
67
- )
68
- . unwrap_throw ( ) ;
67
+ let global = js_sys:: global ( ) . unchecked_into :: < GlobalThis > ( ) ;
68
+ let id = global
69
+ . set_timeout (
70
+ closure. as_ref ( ) . unchecked_ref :: < js_sys:: Function > ( ) ,
71
+ millis as i32 ,
72
+ )
73
+ . unwrap_throw ( ) ;
69
74
70
75
Timeout {
71
76
id : Some ( id) ,
@@ -139,7 +144,8 @@ impl Drop for Interval {
139
144
/// `clearInterval` directly.
140
145
fn drop ( & mut self ) {
141
146
if let Some ( id) = self . id {
142
- clear_interval ( id) ;
147
+ let global = js_sys:: global ( ) . unchecked_into :: < GlobalThis > ( ) ;
148
+ global. clear_interval ( id) ;
143
149
}
144
150
}
145
151
}
@@ -162,11 +168,13 @@ impl Interval {
162
168
{
163
169
let closure = Closure :: wrap ( Box :: new ( callback) as Box < dyn FnMut ( ) > ) ;
164
170
165
- let id = set_interval (
166
- closure. as_ref ( ) . unchecked_ref :: < js_sys:: Function > ( ) ,
167
- millis as i32 ,
168
- )
169
- . unwrap_throw ( ) ;
171
+ let global = js_sys:: global ( ) . unchecked_into :: < GlobalThis > ( ) ;
172
+ let id = global
173
+ . set_interval (
174
+ closure. as_ref ( ) . unchecked_ref :: < js_sys:: Function > ( ) ,
175
+ millis as i32 ,
176
+ )
177
+ . unwrap_throw ( ) ;
170
178
171
179
Interval {
172
180
id : Some ( id) ,
0 commit comments