dimanche 30 août 2020

30 days from first visit Javascript

I need to show a modal on my Webpage to the users after 30 days of the first visit.

I made a cookie which expires after 1 year of the first visit, I tried to subtract 335 days from the expiration date to have 30 days from creation date but it doesn't work.

Here's my cookie code

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 "here";
    i=document.cookie.indexOf(" ",i)+1;
    if (i==0) break;
  }
  return null;
}
function testFirstCookie(){
    var visit=GetCookie("FirstTimeVisitCookie");
    if (visit==null){
      //it does something
   
   }
        var expire=new Date();
       expire=new Date(expire.getTime()+(1000 * 60 * 60 * 8760)); //expires in 1 year
       document.cookie="FirstTimeVisitCookie=here; expires="+expire + "; path=/";

       var month = new Date(expire.getTime() - (1000 * 60 * 60 * 8040)); // I substracted 335 days from expiration date to get 30 days from the creation of the cookie
        var time_now = new Date();
       time_now  = time_now.getTime();

        if (time_now >= month) {

            //shows a popup
         }
            
       
}
$(document).ready(function(){
    testFirstCookie();
});

Any help would be much appreciated!




Aucun commentaire:

Enregistrer un commentaire