A
A
Aleksandr2016-03-18 12:13:47
JavaScript
Aleksandr, 2016-03-18 12:13:47

Hide block after three impressions?

Good afternoon. Please help me to modernize the code a bit.
It is necessary that the block is shown three times and then hidden.
I'm using js-cookie
I can't figure out how to record the increment of a variable.
here is the js code

var cookieNum = 0;
    $(function(){

    if (Cookies.get('disable_banner') === undefined) {
        setTimeout(function() {
            $('.block1').show();
        },2000)
    }
    alert(cookieNum)
    if (cookieNum == 3) {
    	Cookies.set('disable_banner', 1, { expires: 365 });
    }
})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-03-18
@Sashjkeee

Write a counter to the cookie and check, update:

var cookieCounter = Cookies.get('disable_banner');

if (cookieCounter && cookieCounter < 3) {
    /* Показываем блок */
    cookieCounter++;
    Cookies.set('disable_banner', cookieCounter, { expires: 365 });
} else {
    Cookies.set('disable_banner', 0, { expires: 365 });
}

A
amf1k, 2016-03-18
@amf1k

var cookieNum = 0;
    $(function(){

    if (Cookies.get('disable_banner') === undefined) {
        setTimeout(function() {
            $('.block1').show();
cookieNum ++:
        },2000)
    }
    alert(cookieNum)
    if (cookieNum == 3) {
    	Cookies.set('disable_banner', 1, { expires: 365 });
    }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question