Skip to content

Instantly share code, notes, and snippets.

@dineshdeveloper1
Created January 6, 2022 08:42
Show Gist options
  • Save dineshdeveloper1/ba732e061b77dcb6475087e83378e507 to your computer and use it in GitHub Desktop.
Save dineshdeveloper1/ba732e061b77dcb6475087e83378e507 to your computer and use it in GitHub Desktop.
Cookie for 24 hours
// add a cookie for 24 hours
function writeCookie(key, value, hours) {
var date = new Date();
date.setTime(+ date + (hours * 3600000));
window.document.cookie = key + "=" + value + "; expires=" + date + "; path=/";
return value;
};
writeCookie("cookie_name", "1", 24);
// check if cookie exists
const cookieExists = getCookie('cookie_name');
if (!cookieExists == != "") {
console.log('cookie is exists')
}else{
console.log('cookie is exists')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment