0

In the code below, my return function inside the other function doesn't get run right away, but the first function is. My goal is to have the whole function running immediately.

var windowCheck = (function () {
                    var switcher = false;
                    return function () {
                        console.log(switcher);
                        if ($window.innerWidth > 480) {
                            if (!switcher) {
                                element.perfectScrollbar({
                                    wheelSpeed: scope.wheelSpeed || 50,
                                    wheelPropagation: $parse(attrs.wheelPropagation)() || false,
                                    minScrollbarLength: $parse(attrs.minScrollbarLength)() || false
                                });
                                console.log('Plugin On');
                                switcher = true;
                            }

                        }
                        else {
                            if (switcher) {
                                console.log('Plugin Off');
                                element.perfectScrollbar('destroy');
                                switcher = false;
                            }
                        }
                    };
                }());

3 Answers 3

1

The function does indeed run the first time -- an anonymous function runs and it's result is --

Another function!

This 'second' function is then assigned to the value of windowCheck.

And windowCheck won't run until called.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, how about when I add another set of parentheses after the returning function, it actually runs but then i can't call it again.
Corrrect.. because when you add the second set of parens you are causeing it to invoke, and the return value of that function, undefined i this case, gets assigned to windowCheck. IIFE's are great for one-shot runs, but if you want to be able to run it a second time, you have to assign it to a variable and then call the function.
0

Simply run it after defining it and avoid all the anonymous function mess:

var windowCheck = function() {
    ...
}
windowCheck();

1 Comment

They are using the closure to house the switcher variable and keep it private. So, there is some point to the closure.
0

The code as you have it now defines the windowCheck function and uses a closure to house a private switcher variable. That is all fine. If you want the function that gets defined to be executed, then you just have to add this to the end of what you already have:

windowCheck();

in order to actually execute it immediately.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.