在刷新时显示弹出窗口并重置计时器 [英] Show popup and reset timer on refresh

查看:91
本文介绍了在刷新时显示弹出窗口并重置计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向未登录的用户显示一个弹出窗口.我使用javascript和PHP进行此操作.

I am showing a popup for users that are not logged in. I do this using javascript and 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代码如下

Javascript code as below

$(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分钟后显示.

I want to show the popup after 5 minutes for the first time for a visitor when he visits the site. But when the same visitor refresh the page or come again in future I want to show the popup instantly rather after 5 minutes.

请问如何在上述代码中实现该目标.请帮忙.

How can I achieve that in the above code please. Please help.

谢谢

推荐答案

我在朋友的帮助下得到了答案.Cookie的设置应如下所示

I got the answer from my self with the help of my friend .The cookie should set like below

<?php
setcookie("visited",true);

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

此处将$ popup_time变量设置为如下所示的功能javascript代码

Here $popup_time variable is set to the function javascript code like below

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

就是这样:).我很沮丧,没有人在这里给我正确的方法.

And that's it :) . I am frustrated that no one given me a right way here.

无论如何谢谢

这篇关于在刷新时显示弹出窗口并重置计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆