【问题标题】:Detect inactivity with iframe使用 iframe 检测不活动
【发布时间】:2018-05-28 04:54:38
【问题描述】:

我创建了一个网站,在 iframe 中显示了 three.js 模型。每当用户处于非活动状态 x 分钟时,我都会尝试将用户重定向回主页(index.html)。我已经让它与一些 java 脚本一起工作,但问题是它只在我在 iframe 之外处于活动状态时才有效。当我实际旋转模型并与之交互时,它仍然会重定向我。我一直在研究这个并没有找到解决方案。我试过用

调用 iframe 中的函数
onload="setup();"

但这并没有奏效,还有很多其他的。这是我的代码,

JavaScript

var timeoutID;

        function setup() {
            this.addEventListener("mousemove", resetTimer, false);
            this.addEventListener("mousedown", resetTimer, false);
            this.addEventListener("mouseover", resetTimer, false);
            this.addEventListener("mouseout", resetTimer, false);
            this.addEventListener("keypress", resetTimer, false);
            this.addEventListener("DOMMouseScroll", resetTimer, false);
            this.addEventListener("mousewheel", resetTimer, false);
            this.addEventListener("touchmove", resetTimer, false);
            this.addEventListener("MSPointerMove", resetTimer, false);

            startTimer();
        }

        function startTimer() {
            // wait 5 seconds before calling goInactive
            timeoutID = window.setTimeout(goInactive, 5000);
        }

        function resetTimer(e) {
            window.clearTimeout(timeoutID);

            goActive();
        }

        function goInactive() {
            // do something
            window.location = "index.html";
        }

        function goActive() {
            // do something

            startTimer();
        }

HTML

<main role="main" class="col-md-9 ml-sm-auto col-lg-12 px-0">

            <!-- Featured Content  -->

              <div class="embed-responsive embed-responsive-16by9">
                  <div id="test">

                      <iframe style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none;
                   margin:0; padding:0; overflow:hidden; z-index:0;" class="embed-responsive-item" src="models/model.html" onload="setup();" allowfullscreen></iframe>
                  </div>

              </div>

        </main>

【问题讨论】:

    标签: javascript html iframe responsive-design three.js


    【解决方案1】:

    事件不会通过 iframe 的“障碍”冒出来。我不确定您的用例是什么,但 iframe 似乎并不是实现这一目标的最佳方式。

    但是,如果有必要,那么我建议监听 iframe 内容何时加载了 iframe.onload 事件(或为 load 添加事件监听器),然后在您的 @987654324 上添加必要的点击事件@(只有当脚本来自同一来源时,您才能访问)。

    const frame = document.createElement('iframe');
    document.body.appendChild(frame);
    frame.addEventListener('load', function() {
    
      frame.contentWindow.addEventListener("mousemove", resetTimer, false);
      frame.contentWindow.addEventListener("mousedown", resetTimer, false);
      //..other events...
    
    });
    
    // ...
    frame.src = ".../file.html";
    

    【讨论】:

    • 当你说“添加必要的点击事件”时,我是把我的代码放在“mouseover..etc”部分还是创建一个新函数?
    • 您应该可以使用相同的resetTimer 函数。我已经更新了示例。
    • hmm 我在代码中添加了重置功能,但这次它根本没有重定向。我已经尝试在其中添加我的代码和你的代码,结果相同。
    • 没有一组我可以轻松运行的代码很难,但如果它现在根本没有重定向,那么听起来你的时间正在更频繁地重置。我会在重置计时器函数中添加打印语句,以查看哪些回调被触发。
    • 它什么也没做,因为没有触发“加载”功能。我在这里修改了笔:codepen.io/anon/pen/MGNNGX。请注意,我通过srcdoc 向 iframe 添加了内容,这会触发加载。如果您在 iframe 中继续单击 etc,它将永远不会打印它正在重定向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 2017-03-30
    相关资源
    最近更新 更多