if (window.DeviceOrientationEvent) { window.addEventListener('deviceorientation', function hd(eventData) { d =eventData.gamma; window.removeEventListener('deviceorientation',hd,false); },false); }
怎么让全局使用呢
1
moult 2017 年 4 月 2 日 via iPhone
办法一,在事件函数的外面,也就是全局位置进行声明。
办法二, window.foo = bar; |
2
moult 2017 年 4 月 2 日 via iPhone
直接赋值,不用 var 的话,好像是全剧的,但是感觉不是很优雅。
|
3
des 2017 年 4 月 2 日 via Android
用闭包即可,一楼方法也可以
(function(window){ return function hd(eventData) { d =eventData.gamma; window.removeEventListener('deviceorientation',hd,false); }})() |