Reputation: 2777
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;
}
}
};
}());
Upvotes: 0
Views: 107
Reputation: 707158
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.
Upvotes: 0
Reputation: 23863
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.
Upvotes: 1
Reputation: 37701
Simply run it after defining it and avoid all the anonymous function mess:
var windowCheck = function() {
...
}
windowCheck();
Upvotes: 0