我希望每个会话只显示一次弹出窗口,一段时间后过期。有人能帮助我吗?
function PopUp(){
$('.home-popup').fadeIn(500);
}
setTimeout(function(){
PopUp();
},1000); // 1000 to load it after 1 second from page load
$('.close-popup-btn').click(function() {
$('.popup').fadeOut(300);
});
答案 0 :(得分:6)
您也可以使用localstorage。要设置存储项:localStorage.setItem('myPopup','true');
并检查它,您可以执行以下操作:
var poppy = localStorage.getItem('myPopup');
if(!poppy){
function PopUp(){
$('.home-popup').fadeIn(500);
}
setTimeout(function(){
PopUp();
},1000); // 1000 to load it after 1 second from page load
$('.close-popup-btn').click(function() {
$('.popup').fadeOut(300);
});
localStorage.setItem('myPopup','true');
}
答案 1 :(得分:3)
我会在第一次访问时设置一个带有popupSent
值的cookie,并在调用PopUp
函数之前检查cookie是否存在。
这是一个粗略的实现,其中包含cookie辅助函数:
Set cookie and get cookie with JavaScript
function PopUp(){
$('.home-popup').fadeIn(500);
createCookie('popup','1');
}
if(readCookie('popup')){
// 1000 to load it after 1 second from page load
setTimeout(PopUp,1000);
}
$('.close-popup-btn').click(function() {
$('.popup').fadeOut(300);
});
答案 2 :(得分:0)
如果按“每个会话”表示“每页加载”:
$(document).ready(function(){
function PopUp(){
$('.home-popup').fadeIn(500);
}
setTimeout(function(){
PopUp();
},1000); // 1000 to load it after 1 second from page load
$('.close-popup-btn').click(function() {
$('.popup').fadeOut(300);
});
});