2015-10-28 21 views
-1

我正在顯示未登錄的用戶的彈出窗口。我使用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分鐘後立即顯示彈出窗口。

我該如何在上面的代碼中實現這一點。請幫忙。

謝謝

+0

'cookies'會幫你。 –

回答

0

我從我自己和我的朋友的幫助下回答.The餅乾應該設置如下圖所示

<?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?>); 

就是這樣:)。我很沮喪,沒有人在這裏給我一個正確的方法。

無論如何謝謝

0

這裏有關於如何實現這一點的想法。如果一個cookie is_first_time設置

2A

  1. 用戶打開網頁
  2. 檢查。如果未設置,則在5分鐘後顯示彈出窗口,並設置is_first_time cookie
    2b。如果已經設置顯示彈出瞬間

+0

請你能給我確切的代碼。我對cookie不太瞭解。如何設置cookie以及設置位置plz –

+0

如何創建和讀取cookie中的值? http://stackoverflow.com/q/4825683/1288198 –

+0

正如我所說我不是在java或PHP的專家。你能讓我知道如何修改我的PHP或Java代碼嗎?並給我正確的代碼plz在這裏 –

相關問題