0

I'm stuck at a very annoying problem, it's been bugging me since yesterday.

I have a script to hide my 'intro' by adding a class that contains 'display:none', when a user is still in the same session. Yet, my else if won't work whenever I refresh the page. Copying my code and pasting it in the console however, does work.

jQuery(function($) { // this does the trick and also makes sure jQuery is not conflicting with another library
        if (!window.sessionStorage.gateIntro) {
            $('.intro-wrapper').addClass('intro-wrapper--show');
            $(document).on('click', '.intro-button', function(){
                jQuery('.intro-wrapper').addClass('intro-wrapper--hide');
                sessionStorage.gateIntro = 1;
                setTimeout(function() {
                    $('.container').fadeIn('slow');
                }, 1000);
            });

        // INTRO TIMER
        setInterval(function(){

            var random = Math.floor(Math.random() * 90 + 10);
            var random2 = Math.floor(Math.random() * 90 + 10);
            var random3 = Math.floor(Math.random() * 90 + 10);

            var number = "<p class='number'>" + random + " </p>";
            var number2 = "<p class='number'>" + random2 + " </p>";
            var number3 = "<p class='number'>" + random3 + "</p>";

            $('.intro-timer').empty().append(number, number2, number3);

        }, 100);
    }

    else if (window.sessionStorage.gateIntro) {

        console.log('there is a session key!');

        $('.intro-wrapper').addClass('intro-wrapper--hidden');

        $('.container').fadeIn('slow');


    }
});

A few things to keep in mind;

  • I'm using wordpress and angular, wordpress just as my API to fetch JSON
  • I made sure my script.js is loaded after the rest of my js in my HTML
  • Running this script normally does console log 'there is a session key!'
  • It's only the .addClass() and .fadeIn() that doesn't work!
  • .intro-wrapper--hide animates my div 150 viewport height to the top, .intro-wrapper-hidden sets it to display none

** EDIT **

My question was being downvoted due to someone thought it was a duplicate to another question about session storage not working ( sessionStorage isn't working as expected ) , but it has nothing to do with session storage, it doesn't run my addClass() and fadeIn() at all.

If someone could help me out and clear things up to me would be great :)

Thanks in advance!

Community
  • 1
  • 1
Fabian Tjoe A On
  • 1,383
  • 5
  • 18
  • 41

1 Answers1

0

You're problem is likely that you're trying to access the DOM before the document is fully ready. width these lines:

$('.intro-wrapper').addClass('intro-wrapper--hidden');
$('.container').fadeIn('slow');

I'd suggest naming your function and calling it once that is done.

// Call Your Function once the DOM is ready
$( document ).ready( function() { setupIntro(); } );

// Name Your Function
function setupIntro() {
    if (!window.sessionStorage.gateIntro) {
        $('.intro-wrapper').addClass('intro-wrapper--show');
        $(document).on('click', '.intro-button', function(){
            jQuery('.intro-wrapper').addClass('intro-wrapper--hide');
            sessionStorage.gateIntro = 1;
            setTimeout(function() {
                $('.container').fadeIn('slow');
            }, 1000);
        });

        // INTRO TIMER
        setInterval(function(){
            var random = Math.floor(Math.random() * 90 + 10);
            var random2 = Math.floor(Math.random() * 90 + 10);
            var random3 = Math.floor(Math.random() * 90 + 10);

            var number = "<p class='number'>" + random + " </p>";
            var number2 = "<p class='number'>" + random2 + " </p>";
            var number3 = "<p class='number'>" + random3 + "</p>";

            $('.intro-timer').empty().append(number, number2, number3);

        }, 100);
    }
    else if (window.sessionStorage.gateIntro) 
    {
        $('.intro-wrapper').addClass('intro-wrapper--hidden');
        $('.container').fadeIn('slow');
    }
}

This will ensure you're not trying to access DOM elements before they exist.