[英]How can I tell if iPhone is returning from background in javascript?
I know that the iPhone stops javascript from running when an app is sent to the background (ie pressing the home button while the app is running), but I would like to be able to detect if this has happened when the javascript starts up again when the app is reactivated.我知道当应用程序被发送到后台时(即在应用程序运行时按下主页按钮),iPhone 会停止运行 javascript,但我希望能够检测当 javascript 再次启动时是否发生了这种情况该应用程序被重新激活。
One solution I've been trying is to have an iterator constantly running to "check in" and then running a check against that to tell if the app has gone to background.我一直在尝试的一种解决方案是让迭代器不断运行以“签入”,然后对其进行检查以判断应用程序是否已进入后台。
var lastCheckinTime = new Date().getTime();
function checkin(){
lastCheckinTime = new Date().getTime();
}
setIterator( checkin, 1000 );
// Later, some code that needs to know if iphone went to background
var now = new Date().getTime();
if( (now - lastCheckinTime) > 1100 ) {
// run sent to background code
Is there a better way to do this?有一个更好的方法吗? The problem I've found with this method is that it doesn't work if the user quickly closes and reopens the app, but I haven't figured out a better way of detecting this yet.
我发现这种方法的问题是,如果用户快速关闭并重新打开应用程序,它就不起作用,但我还没有想出更好的检测方法。
You can use the 'pageshow' event.您可以使用“pageshow”事件。 Visit this page on your phone, http://jsfiddle.net/vbuDh/
在您的手机上访问此页面, http://jsfiddle.net/vbuDh/
window.addEventListener('pageshow', function(ev){
alert('dasd');
},false);
This will fire anytime the tab gains focus, or mobile Safari is reactivated on this page.这将在标签获得焦点时触发,或者在此页面上重新激活移动 Safari。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.