Home > Mobile >  fadeIn Element just once in a session
fadeIn Element just once in a session

Time:07-18

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

CodePudding user response:

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();
}

CodePudding user response:

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

Did work - nevermind then.

  • Related