javascript android window.onorientationchange 不断触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7166127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript android window.onorientationchange fires continuously
提问by mheavers
I'm trying to detect orientation changes on mobile devices and trigger a function once an orientation has been completed, but the code inside the function is continuously firing in Android and I'm not sure why. Here's my code:
我正在尝试检测移动设备上的方向变化,并在方向完成后触发一个函数,但函数内部的代码在 Android 中不断触发,我不知道为什么。这是我的代码:
var supportsOrientationChange = "onorientationchange" in window;
var orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
window.addEventListener(orientationEvent,
function() { alert ('orientation changed'); },
false
);
Does anyone know how to write this so that it only triggers once, after orientation change has been completed?
有谁知道如何编写它以便在方向更改完成后只触发一次?
回答by Jasper
I used a work-around for this in my app. I added an event listener for orientationchange and then set a timeout so the orientationchange could occur and I could get a new width value that actually reflected the width after the orientation change.
我在我的应用程序中为此使用了解决方法。我添加了一个用于orientationchange 的事件侦听器,然后设置一个超时,以便可以发生orientationchange,并且我可以获得一个新的宽度值,该值实际反映了方向更改后的宽度。
var device_width = 0;
$(document).ready(function () {
var oc_timer;
$(window).bind('orientationchange', function () {
clearTimeout(oc_timer);
oc_timer = setTimeout(function () {
device_width = $(window).width();
}, 500);
});
});
I am not positive this will solve your continuously firing function problem but this is a solution I found to work.
我并不肯定这会解决您的连续触发功能问题,但这是我发现可行的解决方案。
回答by user1759671
I found a way to do this in case others are still having the same issue, check it out:
我找到了一种方法,以防其他人仍然遇到同样的问题,请查看:
function onOrientationChange() {
if (typeof(this.orientation) === "undefined"){
if (window.orientation == -90 || window.orientation == 90) {
this.orientation = "landscape";
} else {
this.orientation = "portrait";
}
// alert ("in 1");
// you code here for change
}
if ((window.orientation == -90 || window.orientation == 90) && (this.orientation == "portrait")) {
this.orientation = "landscape";
// alert ("in 2");
// you code here for change
}
if ((window.orientation == 0 || window.orientation == 180) && (this.orientation == "landscape")) {
this.orientation = "portrait";
// alert ("in 3");
// you code here for change
}
}
回答by techdude
Simple solution for less detailed applications
用于不太详细的应用程序的简单解决方案
//bind orientation change event
$(window).bind("orientationchange", orientationChange);
//later,
function orientationChange(){
//whatever needs to happen when it changes.
alert("changed");
}
You could also use $.on() instead if you need to pass information.
如果需要传递信息,也可以使用 $.on() 代替。
In browsers that do not support it, the event does not fire. You could then write a custom detection script as a fallback to fire the event if the orientation changes on a device that does not support onorientationchange.
在不支持它的浏览器中,该事件不会触发。然后,您可以编写自定义检测脚本作为回退,如果在不支持 onorientationchange 的设备上发生方向更改,则触发事件。