简体   繁体   English

如何判断 iPhone 是否从 javascript 后台返回?

[英]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.

相关问题 如何用Javascript区分iPhone,iPad和iPod? - How can I tell the difference between an iPhone, iPad and iPod in Javascript? 我怎样才能知道所有Angular命令是从Javascript完成的? - How can I tell all Angular commands are complete from Javascript? 如何判断ReactJS是否处于JavaScript开发模式? - How can I tell whether ReactJS is in development mode from JavaScript? 如何判断Javascript对象是否为文件? - How can I tell if a Javascript object is a File? 如何判断我是否点击了 Google Graphs ScatterChart 的背景? - How Can I Tell If I Clicked the Background of a Google Graphs ScatterChart? javascript代码可以判断浏览器是否在后台吗? - Can javascript code tell if the browser is in the background? 如何获得emacs来区别编写PHP,HTML和JavaScript时的区别? - How can I get emacs to tell the difference from when I am writing PHP, HTML, and JavaScript? 如何从 javascript 文件更改背景颜色 - How can i change the background color from a javascript file 如何在JavaScript中以编程方式判断从哪个文件中将特定CSS类应用于HTML元素? - How can I programmatically tell, in JavaScript, from which file a particular CSS class is getting applied to an HTML element? 我可以告诉 Javascript 从另一个 JS 文件调用方法吗? - Can I tell Javascript to call a method from another JS file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM