如何通过javascript设置10秒后的cookie
我真的需要你来解决这个问题。我尝试了几种方法,但我的大脑不再工作了。我做了一个函数,在JavaScript中设置cookie,但是这样,当窗口加载时,cookie(如何通过javascript设置10秒后的cookie,javascript,jquery,bootstrap-modal,Javascript,Jquery,Bootstrap Modal,我真的需要你来解决这个问题。我尝试了几种方法,但我的大脑不再工作了。我做了一个函数,在JavaScript中设置cookie,但是这样,当窗口加载时,cookie(visit)将立即设置,但是我需要在10秒后设置cookie,用户停留在网站页面上。你能帮帮我吗,我的朋友们? 此外,通过这个代码,我想显示一个模式,其中iakarneta是该模式的ID jQuery(document).ready(function($) { function getCookieVal(offset) {
visit
)将立即设置,但是我需要在10秒后设置cookie,用户停留在网站页面上。你能帮帮我吗,我的朋友们?此外,通过这个代码,我想显示一个模式,其中iakarneta是该模式的ID
jQuery(document).ready(function($) {
function getCookieVal(offset) {
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function SetCookie(name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DisplayInfo() {
var expdate = new Date();
var visit;
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
if (!(visit = GetCookie("visit")))
visit = 0;
visit++;
SetCookie("visit", visit, expdate, "/", null, false);
if (visit == 1) {
$('#hereiakarneta').modal({ show: true });
}
if (visit == 2) {
$('#hereiakarneta').modal({ show: true });
}
if (visit == 3) {
$('#hereiakarneta').modal({ show: true });
}
}
//window.onload = DisplayInfo
$(window).on("load", DisplayInfo);
});
jQuery(文档).ready(函数($){
函数getCookieVal(偏移量){
var endstr=document.cookie.indexOf(“;”,offset);
如果(endstr==-1)
endstr=document.cookie.length;
返回unescape(document.cookie.substring(offset,endstr));
}
函数GetCookie(名称){
var arg=name+“=”;
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
而(我
HTML
&时代;
在应用商店下载
接近
非常感谢大家为页面加载事件添加超时,如下所示:
window.addEventListener('load',function(){
setTimeout(function(){
document.cookie = "hasBeenHereFor10Seconds=true";
},10000)
});
向页面的加载事件添加超时,如下所示:
window.addEventListener('load',function(){
setTimeout(function(){
document.cookie = "hasBeenHereFor10Seconds=true";
},10000)
});
最后,我发现我无法在10秒后将+1添加到live上的cookie(不刷新页面)。所以我改变了我的问题,以另一种方式显示模式(弹出窗口)。所以请看新问题:
在更改我的问题之前,我使用了以下代码和
setTimeout()
,但问题是:当用户打开一个页面时,cookie将立即设置,10秒后将显示模式,因此当用户在10秒之前离开页面时,我想向他展示的三次中的一次模式将丢失:|我需要当用户打开页面时,在10秒后设置cookie,当用户在10秒前离开页面时,不设置cookie
jQuery(document).ready(function($) {
function getCookieVal(offset) {
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function SetCookie(name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DisplayInfo() {
var expdate = new Date();
var visit;
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
if (!(visit = GetCookie("HereIsKarneta")))
visit = 0;
visit++;
SetCookie("HereIsKarneta", visit, expdate, "/", null, false);
//var message;
if (visit < 4) {
//$('#hereiakarneta').modal({ show: true });
setTimeout(function(){
$('#hereiakarneta').modal({
show: true
})
}, 2000);
}
if (visit >= 4) {
$(".dologinfirst").delay(2000).fadeIn(500);
$("#menubutton").click(function(){
$(".dologinfirst").hide();
});
$('body').click(function() {
$(".dologinfirst").hide();
});
}
}
//window.onload = DisplayInfo
$(window).on("load", DisplayInfo);
});
jQuery(文档).ready(函数($){
函数getCookieVal(偏移量){
var endstr=document.cookie.indexOf(“;”,offset);
如果(endstr==-1)
endstr=document.cookie.length;
返回unescape(document.cookie.substring(offset,endstr));
}
函数GetCookie(名称){
var arg=name+“=”;
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
而(我=4){
美元(“.dologinfirst”)。延迟(2000年)。法代因(500);
$(“#菜单按钮”)。单击(函数(){
$(“.dologinfirst”).hide();
});
$('body')。单击(函数(){
$(“.dologinfirst”).hide();
});
}
}
//window.onload=DisplayInfo
$(窗口).on(“