简体   繁体   English

使用 PhoneGap/Cordova InAppBrowser 定时弹出?

[英]Timed popup with PhoneGap/Cordova InAppBrowser?

I'm using PhoneGap (Apache Cordova) with the InAppBrowser plugin (cordova-plugin-inappbrowser).我正在使用 PhoneGap (Apache Cordova) 和 InAppBrowser 插件 (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);
  });
}

While the server side (the url called) looks like:而服务器端(调用的 url)看起来像:

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

Any comment appreciated.任何评论表示赞赏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM