Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  100] [ 4]  / answers: 1 / hits: 37945  / 10 Years ago, sat, january 10, 2015, 12:00:00
var app = angular.module('myapp', []);

app.controller('PopupCtrl', function($scope, $timeout){
$scope.show = 'none';


$scope.mouseover = function(){
console.log('Mouse Enter');
$scope.show = 'block';
};

$scope.mouseout = function(){

console.log('Mouse Leave');
var timer = $timeout(function () {
$scope.show = 'none';
}, 2000);


};

});


When I mouseover a button, a pop up dialog box is show. When I mouseout, the pop up dialog box is going to be hidden in two seconds. The problem come when I mouseover the button for the second time. Even my cursor is still on the button, the pop up dialog box is hide in two second. How to stop the timer when the mouse is over the button again?


More From » angularjs

 Answers
33

The $timeout service returns a promise that can be cancelled using $timeout.cancel(). In your case, you have to cancel the timeout in every button mouse over.


DEMO


JAVASCRIPT


var app = angular.module('myapp', []);

app.controller('PopupCtrl', function($scope, $timeout){
var timer;
$scope.show = false;

$scope.mouseover = function(){
$timeout.cancel(timer);
$scope.show = true;
};

$scope.mouseout = function(){
timer = $timeout(function () {
$scope.show = false;
}, 2000);
};

});

[#68256] Wednesday, January 7, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lara

Total Points: 462
Total Questions: 100
Total Answers: 102

Location: Jersey
Member since Mon, Jun 14, 2021
3 Years ago
lara questions
;