javascript 如何在 10 秒后设置 cookie
how to set cookie after 10 seconds by javascript
我真的需要你来解决这个问题。我尝试了几种方法,但我的头脑不再起作用了。我做了一个在 JavaScript 中设置 cookie 的函数,但是这样,当 window 加载时,cookie (visit
) 立即 将被设置,但我需要在用户停留在网站页面 10 秒后 设置 cookie。你能帮帮我吗,我的朋友们?
此外,通过这段代码,我想显示一个 Modal 并且 hereiakarneta
是该模态的 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);
});
HTML
<!-- Modal -->
<div id="hereiakarneta" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" style="text-align: center;" >Download on app store</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-lg-12"><img src="" class="img-responsive" /></div>
</div>
<div class="row">
<div class="col-xs-4 col-xs-offset-2"><img src="" class="img-responsive" /></div>
<div class="col-xs-4"><img src="" class="img-responsive" /></div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
非常感谢大家
像这样为页面的加载事件添加超时:
window.addEventListener('load',function(){
setTimeout(function(){
document.cookie = "hasBeenHereFor10Seconds=true";
},10000)
});
最后我发现我无法在 10 秒后在实时(不刷新页面)上向 cookie 添加 +1。所以我改变了我的问题,以另一种方式显示模态(弹出窗口)。所以请看新问题:
在更改我的问题之前,我将以下代码与 setTimeout()
一起使用,但问题是:当用户打开一个页面时,将立即设置 cookie,并在 10 秒后显示模态,所以当用户在 10 秒之前离开页面时,我想向他展示模态的那 3 次中的一次将会丢失:|我需要当用户打开页面时,在 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);
});
我真的需要你来解决这个问题。我尝试了几种方法,但我的头脑不再起作用了。我做了一个在 JavaScript 中设置 cookie 的函数,但是这样,当 window 加载时,cookie (visit
) 立即 将被设置,但我需要在用户停留在网站页面 10 秒后 设置 cookie。你能帮帮我吗,我的朋友们?
此外,通过这段代码,我想显示一个 Modal 并且 hereiakarneta
是该模态的 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);
});
HTML
<!-- Modal -->
<div id="hereiakarneta" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" style="text-align: center;" >Download on app store</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-lg-12"><img src="" class="img-responsive" /></div>
</div>
<div class="row">
<div class="col-xs-4 col-xs-offset-2"><img src="" class="img-responsive" /></div>
<div class="col-xs-4"><img src="" class="img-responsive" /></div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
非常感谢大家
像这样为页面的加载事件添加超时:
window.addEventListener('load',function(){
setTimeout(function(){
document.cookie = "hasBeenHereFor10Seconds=true";
},10000)
});
最后我发现我无法在 10 秒后在实时(不刷新页面)上向 cookie 添加 +1。所以我改变了我的问题,以另一种方式显示模态(弹出窗口)。所以请看新问题:
在更改我的问题之前,我将以下代码与 setTimeout()
一起使用,但问题是:当用户打开一个页面时,将立即设置 cookie,并在 10 秒后显示模态,所以当用户在 10 秒之前离开页面时,我想向他展示模态的那 3 次中的一次将会丢失:|我需要当用户打开页面时,在 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);
});