In the below, we created cookie that would expire at midnight (timezone issues could be).
If cookie isn't there or has expired we run ajax else we do nothing.
Also you can see the cookie details from developer tools >> application >> cookies >> your adress (for chrome 61)
$(document).ready(function() { var myCookie = getCookie("MY_cookieName"); if(myCookie == "") { //cookie is not created or has expired $.ajax({ //Do your job }); var createdTime = new Date().toString(); setCookieExpireAtMidnight("MY_cookieName", createdTime); } }); function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } function setCookieExpireAtMidnight(cname, value) { var midnight = new Date(); midnight.setHours(23,59,59,0); var expires = "expires=" + midnight.toString(); document.cookie = cname + "=" + value + ";" + expires + "; path=/"; }
No comments:
Post a Comment