0

I have a function that is run when you mouseover and mouseout of a div. When you mouseout I need the function to wait a second before running. Is there a way to set a timeout on an if else statement to make this work?

HTML:

<div ng-mouseover="menu()" ng-mouseout="menu()"><img src="images/headermenubutton.png" style="height: 73px; width: auto; position: absolute; left: 230px; z-index: 2000;"></div>
<div class="menu" ng-mouseover="menu()" ng-mouseout="menu()" ng-show="dropdown_menu" ng-cloak>
    <a href="about_us.html"><h2 class="lighter" style="padding-top: 10px;">ABOUT US</h2></a>
    <a href="contact_us.html"><h2 class="lighter">CONTACT US</h2></a>
    <a href="/blog"><h2 class="lighter">BLOG</h2></a>
    <a href="contact_us.html#faq"><h2 class="lighter">FAQ'S</h2></a>
</div>

JS:

$scope.menu = function() {
    if($scope.dropdown_menu) {
        setTimeout(function(){ $scope.dropdown_menu = false; }, 1000);
    } else {
        $scope.dropdown_menu = true;
    }
};
3
  • 3
    You're doing it right now. What's the issue? Commented May 12, 2015 at 17:22
  • 2
    It looks like you are using angular. try $timeout Commented May 12, 2015 at 17:22
  • Forgot to load $timeout as a dependency.... My bad!!! Commented May 12, 2015 at 17:39

1 Answer 1

2

As suggested, try $timeout wrapper of angular

Solution with $timeout used:

$scope.menu = function () {
    if ($scope.dropdown_menu) {
        $timeout(function () {
            $scope.dropdown_menu = false;
        }, 1000);
    } else {
        $scope.dropdown_menu = true;
    }
};

https://jsfiddle.net/Lx41yy47/

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

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.