简体   繁体   中英

How to call panel after completing 24 hours in javascript

Hi I want to open a specific panel in javascript after it completes 24 hours. I am trying to do it but its not working. Here is my code:

var currentTime = new Date().getTime();
var visitedTime = Get_Cookie('lastvisitedtime');
if (!visitedTime || visitedTime < (currentTime - 24*60*60*1000) ) {
    alert('visitedTime = ' + visitedTime);
    Set_Cookie("lastvisitedtime", currentTime);

}
else{
    Set_Cookie("lastvisitedtime", currentTime); //first time
}

but it always go to else part. How can I achive it so that it goes to if condition and show alert part.

Any help is appreciated. Thanks

corrected code..

Steps:

  1. Set lastvisitedtime cookie value to 0 in start
  2. call a setInterval() method which will call itself after 1000ms. It will call timeEvent() function after 1000ms.
  3. Apply a if condition to check if visitedTime is 0 or it have some valied value
  4. If visitedTime have some valied time value then Apply if condition to check time difference between visitedTime & currentTime

    DEMO jsfiddle

     // In start set cookie to null setCookie("lastvisitedtime", 0); setInterval(function(){timeEvent()}, 1000); function timeEvent(){ var currentTime = (new Date()).getTime(); var visitedTime = getCookie('lastvisitedtime'); if (visitedTime != 0){ if((currentTime - visitedTime) >= 60*1000){ alert('visitedTime = ' + visitedTime); setCookie("lastvisitedtime", currentTime); } } else{ setCookie("lastvisitedtime", currentTime); //first time } } function getCookie(c_name) { var c_value = document.cookie; var c_start = c_value.indexOf(" " + c_name + "="); if (c_start == -1) { c_start = c_value.indexOf(c_name + "="); } if (c_start == -1) { c_value = null; } else { c_start = c_value.indexOf("=", c_start) + 1; var c_end = c_value.indexOf(";", c_start); if (c_end == -1) { c_end = c_value.length; } c_value = unescape(c_value.substring(c_start,c_end)); } return c_value; } function setCookie(c_name,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; } function checkCookie() { var username=getCookie("username"); if (username!=null && username!="") { alert("Welcome again " + username); } else { username=prompt("Please enter your name:",""); if (username!=null && username!="") { setCookie("username",username,365); } } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM