本文介绍了使用PhoneGap/Cordova InAppBrowser定时弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将PhoneGap(Apache Cordova)与InAppBrowser插件(cordova-plugin-inappbrowser)结合使用.为了显示弹出窗口,我使用以下功能:

I'm using PhoneGap (Apache Cordova) with the InAppBrowser plugin (cordova-plugin-inappbrowser). In order to display a popup window, I'm using the following function:

function popup(url) {
  var win = cordova.InAppBrowser.open(url, "_blank", "location=no");
  win.addEventListener("loadstop", function() {
    var loop = window.setInterval(function() {
      win.executeScript(
        {code: "window.shouldClose"},
        function(values) {
          if(values[0]) {
            win.close();
            window.clearInterval(loop);
          }
        }
      );
    }, 200);
  });
}

我真正需要的是一个函数,其规格为:

What I really need is a function whose spec is:

function Popup(url, maxTimeToWaitIfResponseIsSlow)

有什么想法可以实现这一目标吗?

Any ideas how to achieve this?

推荐答案

因此,经过一些研究和测试,我找到了答案.这是修改后的函数,在客户端具有双重防御":

So I've found an answer after some research and testing. Here is the modified function, with "double defense" in the client side:

function popup(url) {
  var elapsed = 0;
  var loadstopFired = false;
  var win = cordova.InAppBrowser.open(url, "_blank", "location=no");
  setTimeout(function() {if (!loadstopFired) win.close();}, 2000);
  win.addEventListener("loadstop", function() {
    loadstopFired = true;
    var loop = window.setInterval(function() {
      elapsed += 200;
      win.executeScript(
        {code: "window.popupStatus"},
        function(values) {
          if (values[0]=="closed") { win.close(); window.clearInterval(loop); }
          else if ((values[0]!="loaded") && (elapsed>2000)) { win.close(); window.clearInterval(loop); }
        }
      );
    }, 200);
  });
}

服务器端(称为url)的外观如下:

While the server side (the url called) looks like:

...
<body onload="window.popupStatus='loaded';">
  ...
  <img src="close.png" onclick="window.popupStatus='closed';" />
  ...
</body>
...

任何评论表示赞赏.

这篇关于使用PhoneGap/Cordova InAppBrowser定时弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 00:08