Reputation: 335
I am trying to find out how to trigger a pop-up after a certain amount of time within a session, not resetting when user switches pages. I understand that the easiest way is to use a cookie to track the session, but I have yet to figure out how to trigger a function at a certain time-stamp of the session.
This is a script that I think would be useful: https://github.com/js-cookie/js-cookie
Any suggestions on how to approach this? Thank you in advance.
Upvotes: 0
Views: 501
Reputation: 335
What I ended up using (variation of Husam's answer):
if (Cookies.get('sessionPopup')) {} else {
var cookieTime = new Date().getTime();
Cookies.set('sessionPopup', cookieTime, { expires: 1 });
}
var loop = setInterval(function(){
var initialTime = Cookies.get('sessionPopup');
var triggerTime = new Date();
triggerTime = new Date(triggerTime .getTime() - 1000*60);
if(triggerTime.getTime() >= initialTime ){
console.log('Popup is up!');
clearInterval(loop);
}
}, 1000)
Upvotes: 0
Reputation: 36
First, Set the cookie with the initial date.
Second, in your app
var minute = 1000 * 60;
var loop = setInterval(function(){
var currentDate = new Date();
var initialDate = new Date(Cookies.get('date'));
if(initialDate - currentDate >= minute){
Popup();
clearInterval(loop);
}
}, 1000)
Upvotes: 2