Remove div forever if cookie expires

I trying to get a cookie to last two minutes (example below) and when the two minutes expires to stop showing a div and never show it again. Below the “div” would be where it says “do stuff here”.

The code below doesn’t automatically stop the div from showing and then on refresh it creates a new cookie!

This might not be the right kind of code? How can I accomplish this?

function createCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*2*60*1000));
        expires = "; expires=" + date.toUTCString();
    document.cookie = name + "=" + value + expires + "; path=/";

function readCookie(name) {
    var nameEQ = name + "=";
    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,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    return null;

function eraseCookie(name) {

var x = readCookie('ppkcookie')
if (x) {
//do stuff here


I would do it the other way round.

  • Show div on page load
  • Check for cookie
  • If cookie present, hide div
  • If no cookie present, start a timer for 2 mins (using e.g. setTimeout)
  • When timer reaches 0, hide div and set a cookie

Would that work for what you are trying to accomplish?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.