I have a little script that checks for cookies and I was wondering how I could do it so that it did this automatically rather than on page refresh?
My code is:
if(!$.cookie("popbox")){
if(!$.cookie("visited") && !$.cookie("firstvisit")){
var expirydate = new Date();
expirydate.setTime(expirydate.getTime() + (1 * 60 * 1000));
$.cookie("popbox", "feedback_popup", { expires: expirydate });
$.cookie("firstvisit", 1, { expires: 9999 });
} else {
if(!$.cookie("visited") && $.cookie("firstvisit")){
showPopuptimer();
}
}
}
If you mean you want the code to run periodically while the page is being shown, you can use
setInterval, which will call the function you give it on a specific interval (more or less), or a chained series ofsetTimeoutcalls (setTimeoutcalls the function you give it once; you’d then have the function callsetTimeoutagain). (Those links are to Mozilla documentation, but these calls exist in all browsers.)I tend to prefer chained serieses of
setTimeoutcalls because it’s harder to have them get away from you;setIntervaljust keeps happening until/unless you callclearIntervalon the handle it returns.So for instance:
You might need an
ifin there for if you have some kind of pop-up showing and don’t want to do the check while the pop-up is there.