Stopping the $timeout - AngularJS
Stopping the $timeout - AngularJS
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);
};
});
当我将鼠标悬停在按钮上时,会显示一个弹出对话框。当我鼠标退出时,弹出的对话框将在两秒钟内隐藏。当我第二次将鼠标悬停在按钮上时,问题就来了。即使我的光标仍然在按钮上,弹出对话框也在两秒钟内隐藏。当鼠标再次悬停在按钮上时如何停止计时器?
$timeout
服务返回一个承诺,可以使用 $timeout.cancel()
取消该承诺。在您的情况下,您必须取消每个按钮鼠标的超时。
演示
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);
};
});
相关文章:
- $timeout如何在angularJs中工作
- 为什么 angularjs 插件无缘无故地使用 $timeout
- AngularJS$带$timeout的有状态过滤器陷入循环
- 如何在AngularJS点击触发器之前设置Timeout
- AngularJS $q.all timeout
- Stopping the $timeout - AngularJS
- 使用 AngularJS $timeout “this” 而不是 $scope
- jQuery-Chains in an AngularJS $timeout
- AngularJS和Firebase“$timeout未定义”
- $timeout AngularJS 应用程序中未定义的错误
- AngularJS $timeout in for 循环生成错误的值
- AngularJS:使用$timeout递归调用函数时的RangeError
- AngularJS钟表工厂,$timeout困境
- 即使使用$timeout,View也不会在angularjs中更新
- AngularJS Ionic$timeout显示分钟和秒
- Angularjs$timeout生成无限循环
- AngularJS $timeout循环只触发一次
- AngularJS, timeout没有取消
- Angularjs $timeout not executing
- AngularJS $timeout的工作方式不同