TheCodingTentacle
TheCodingTentacle

Reputation: 137

fadeIn Element just once in a session

I have a function:

function loremIpsum() {
$('.element').fadeIn()
}

to show a hidden Element - The issue is, that a user can go back with a second button and call the loremIpsum() function again, but the function should be called just once. Can I limit that in someway ? I couldn't find a Post for that here.

I tried this:

function loremIpsum() {
    var executed = false;
    return function() {
        if (!executed) {
            executed = true;
            $('.element').fadeOut()

        }
    }
}

But that didn't work

Upvotes: 1

Views: 52

Answers (2)

Aastha Bist
Aastha Bist

Reputation: 356

You can use sessionStorage to keep track of executed boolean value for the session.

function loremIpsum() {
    sessionStorage.setItem('executed', true);
    $('.element').fadeIn();
}

And in the caller, you can check:

if (!(sessionStorage.getItem('executed') === "true")) { //since sessionStorage stores items as strings
    loremIpsum();
}

Upvotes: 1

TheCodingTentacle
TheCodingTentacle

Reputation: 137

function loremIpsum() {
    loremIpsum= function() {};
    $('.element').fadeIn()
}

Did work - nevermind then.

Upvotes: 0

Related Questions