我具有一个div的鼠标和鼠标之路时运行的函数。鼠标鼠标时,我需要该功能在运行之前等待一秒钟。有没有办法在其他语句上设置超时以进行此工作?
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;
}
};
如建议,尝试$ timeout包装器
使用$timeout
使用的解决方案:
$scope.menu = function () {
if ($scope.dropdown_menu) {
$timeout(function () {
$scope.dropdown_menu = false;
}, 1000);
} else {
$scope.dropdown_menu = true;
}
};
https://jsfiddle.net/lx41yy47/