【问题标题】:Show Modal after user hasnt interact with the page in x minutes用户在 x 分钟内没有与页面交互后显示模态
【发布时间】:2018-08-06 21:52:51
【问题描述】:

我想知道是否有合理的简单方法可以在页面未向用户显示一段时间后显示模式。所以我有一个页面,我没有关闭它,但我转到另一个页面,一旦发生这种情况,如果我在不到 5 分钟内没有回到页面,则会显示一个模式告诉我“我离开了这么久,我应该刷新它”所以当我回到那个页面时,我看到了那个模式。

显然我只是“请求”时间码,已经得到了模态,这里是:

    <div style="padding: 20px;"id="id01" class="w3-modal">
        <div class="w3-modal-content w3-animate-top w3-card-4">
            <header class="w3-container w3-teal"> 
                <span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-display-topright">&times;</span>
            </header>
            <div style="width: 100%" class="w3-container">
                <p id="captcha_advertencia">¡Vaya!</p>
                <img src="imagenes/captchaIncorrecto.jpeg" />
                <p>Los valores que has introducido no se corresponden con el captcha</p>
                <p>Por favor introduce correctamente los caracteres de la imagen</p>
            </div>
        </div>
    </div>

感谢您的宝贵时间

【问题讨论】:

标签: javascript php html css


【解决方案1】:

这是基于 Thijs 提交的答案。 5 秒不活动后窗口中的任何活动都应显示该消息。 CSS 确定默认情况下不显示消息。

如果您对 jQuery 不太熟悉 - 请注意,您需要链接到 jQuery 库 - 在此处通过 Google CDN 版本包含以下链接的最新版本:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

#id01 {
    display: none;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Exercises</title>
        <!-- using CDN version of jQuery -->
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                // Five seconds.
                var THRESHOLD = 5 * 1000;
                // When was the last mouse movement?
                var lastActive = Date.now();
                $(document).on('mousedown mousemove keypress',function(){ 
                    if (Date.now() > lastActive + THRESHOLD) {
                        $('#id01').attr('style', 'display:block');
                    }
                    // Update last mouse movement timestamp.
                    lastActive = Date.now();
                });
            });
        </script>
    </head>
    <body>
    <p>Mousing over the window after 5 secs of inactivity should show the message</p>
    <div style="padding: 20px;"id="id01" class="w3-modal">
        <div class="w3-modal-content w3-animate-top w3-card-4">
            <header class="w3-container w3-teal"> 
                <span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-display-topright">&times;</span>
            </header>
            <div style="width: 100%" class="w3-container">
                <p id="captcha_advertencia">¡Vaya!</p>
                <img src="imagenes/captchaIncorrecto.jpeg" />
                <p>Los valores que has introducido no se corresponden con el captcha</p>
                <p>Por favor introduce correctamente los caracteres de la imagen</p>
            </div>
        </div>
    </div>
    </body>
</html>

【讨论】:

    【解决方案2】:

    如果你使用 jQuery,你可以使用以下代码:

    // Five seconds.
    var THRESHOLD = 5 * 1000;
    // When was the last mouse movement?
    var lastActive = Date.now();
    
    $(window).on('mousemove', function() {
        // Test if mouse hasn't moved in over 5 seconds.
        if (Date.now() > lastActive + THRESHOLD) {
           // show modal code
        }
        // Update last mouse movement timestamp.
        lastActive = Date.now();
    });
    

    view on codepen

    【讨论】:

    • 修复它并添加到 codepen 的链接。要进行测试,请将鼠标移到白色方块上,然后等待 +5 秒,然后再次移动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多