简体   繁体   中英

Javascript function scope - to use console.log or not?

Two functions:

First: Closes a stickyFooter that is fixed to the bottom of the page onclick of the cross.

jQuery:

jQuery(document).ready(function() {
 function closeSticky() {
    jQuery('.stickyFooter').hide();
      jQuery.cookie('stickyNewsClosed', 'yup', {
        path: '/',
        expires: 30
      });
    }
});

Second: This function fades in/fades out two divs, and stops when there's focus to an input area. What needs to happen now is when the stickyfooter is closed it needs to call the clearTimeout in this separate function:

jQuery(document).ready(function () {

    // check if both divs are visible
    if ((jQuery('.footerPromoBannerWrapper').is(':visible')) && (jQuery('.stickyFooter').is(':visible'))) {

        // Local variable for cancel of fades
        var stickyTimeout;

        // Set sticky as display:none
        jQuery('.stickyFooter').hide();

        // Switch in
        window.switchIn = function () {
            jQuery('.footerPromoBannerWrapper').fadeToggle(function () {
                jQuery('.stickyFooter').fadeToggle(function () {
                    stickyTimeout = setTimeout(function () {
                        window.switchOut();
                    }, 3000);
                });
            });
        };

        // Switch out
        window.switchOut = function () {
            jQuery('.stickyFooter').fadeToggle(function () {
                jQuery('.footerPromoBannerWrapper').fadeToggle(function () {
                    stickyTimeout = setTimeout(function () {
                        window.switchIn();
                    }, 3000);
                });
            });
        };

        stickyTimeout = setTimeout(function () {
            window.switchIn();
        }, 5000);

        jQuery('input#emailsignup').focus(function() {
            clearTimeout(stickyTimeout);

        });

    } // End of both divs are visible if statement
});

Question:

How do I combine both in order to call the timeOut feature as part of the close of the sticky footer? Something like this?

First function amendment:

function closeSticky() {
jQuery('.stickyFooter').hide();
jQuery.cookie('stickyNewsClosed', 'yup', {
path: '/',
expires: 30
});
stopAnimation();
}

Second function amendment:

function stopAnimation() {
jQuery('input#emailsignup').focus(function() {
clearTimeout(stickyTimeout);
});
} // End stopAnimation function
console.log(function stopAnimation());

You have jQuery inside the functions, so i would suggest moving the 2 functions inside the dom ready scope. Your cleartimeout is probably calling in udefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM