刷新时显示弹出窗口并重置计时器


Show popup and reset timer on refresh

我为未登录的用户显示了一个弹出窗口。我使用javascript和PHP来实现这一点。

<?php
if ( ( is_single() || is_front_page() || is_page() ) 
       && !is_page('login') && !is_page('register') && !is_user_logged_in()){
    echo'<div class="overlay-bg">
</div>
<div class="overlay-content popup3">
    <h1>You must login or Register to view this site. do_shortcode("[sp_login_shortcode]");</h1>
</div>';
} 
?>

如下的Javascript代码

$(document).ready(function(){
    // function to show our popups
    function showPopup(whichpopup){
        var docHeight = $(document).height(); //grab the height of the page
        var scrollTop = $(window).scrollTop(); //grab the px value from the top of the page to where you're scrolling
        $('.overlay-bg').show().css({'height' : docHeight}); //display your popup background and set height to the page height
        $('.popup'+whichpopup).show().css({'top': scrollTop+20+'px'}); //show the appropriate popup and set the content 20px from the window top
    }
    // function to close our popups
    function closePopup(){
        $('.overlay-bg, .overlay-content').hide(); //hide the overlay
    }
    // timer if we want to show a popup after a few seconds.
    //get rid of this if you don't want a popup to show up automatically
    setTimeout(function() {
        // Show popup3 after 2 seconds
        showPopup(3);
    }, 4000);

});

我想在访问者访问网站时,第一次在5分钟后显示弹出窗口。但当同一个访问者刷新页面或将来再次出现时,我想立即显示弹出窗口,而不是在5分钟后。

请问我如何在上面的代码中实现这一点。请帮忙。

感谢

在朋友的帮助下,我从自己那里得到了答案。cookie应该设置为如下

<?php
setcookie("visited",true);
if(!empty($_COOKIE['visited']) && $_COOKIE['visited'] == true)
 $popup_time = 0;
 else
 $popup_time = 60000; 
?>

这里$popup_time变量被设置为函数javascript代码,如下

setTimeout(function() {
        // Show popup3 after 2 seconds
        showPopup(3);
    }, <?php echo $popup_time?>);

就这样:)。我很沮丧,因为没有人给我一条正确的路。

无论如何,感谢

这里是关于如何实现这一目标的想法。

  1. 用户打开页面
  2. 检查cookie is_first_time是否设置为

    2a。如果未设置,则在5分钟后显示弹出窗口并设置is_first_time cookie
    2b。如果已经设置,立即显示弹出