作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 AngularJS 间隔功能连续运行的毫秒时间。我创建了一个暂停 和继续 按钮。当点击 Pause 按钮时,它将触发 AngularJS 的 Interval.cancel 功能,如果我点击 continue 按钮,它将触发间隔功能,但问题是当我点击Continue 按钮两次或更多次,然后点击 pause 按钮它不会暂停并且毫秒继续运行但是当 continue 按钮时它会暂停被击中一次。下面是 UI 的屏幕截图。非常感谢您的帮助。
AngularJS 代码
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date();
$scope.startInterval = function(){
$scope.flag = $interval(function(){
$scope.theTime = new Date();
}, 6);
}
$scope.stopInterval= function(){
if($scope.flag)
$interval.cancel($scope.flag);
}
// $scope.startInterval();
});
HTML代码
<div class="col-lg-12 text-center" ng-app="myApp" ng-controller="myCtrl" >
<h3 ><b id="thetime"><span id="acttime" ng-mouseover="stopInterval()" ng-mouseleave="startInterval()" >{{theTime| date:'hh:mm:ss sss a'}}</span> <button class="btn btn-sm btn-success Start" ng-click="stopInterval()" style="margin-bottom: 70;"> Pause </button>
<button class="btn btn-sm btn-info Start" ng-click="startInterval()" style="margin-left: -92;margin-top: 25;"> Continue </button></b></h3>
</div>
最佳答案
试试这个,它的工作
当计时器停止时,使 $scope.flag
未定义。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date();
$scope.flag = 'undefined';
$scope.startInterval = function(){
if($scope.flag == 'undefined'){
$scope.flag = $interval(function(){
$scope.theTime = new Date();
}, 6);
}
}
$scope.stopInterval= function(){
if(!angular.isUndefined($scope.flag)){
$interval.cancel($scope.flag);
$scope.flag = 'undefined';
}
}
// $scope.startInterval();
});
<!DOCTYPE html>
<html lang="en-us" ng-app="myApp">
<head>
<title>Angularjs</title>
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
</head>
<body>
<div ng-controller="myCtrl">
<div class="col-lg-12 text-center" ng-app="myApp" ng-controller="myCtrl" >
<h3 ><b id="thetime"><span id="acttime" ng-mouseover="stopInterval()" ng-mouseleave="startInterval()" >{{theTime| date:'hh:mm:ss sss a'}}</span> <button class="btn btn-sm btn-success Start" ng-click="stopInterval()" style="margin-bottom: 70;"> Pause </button>
<button class="btn btn-sm btn-info Start" ng-click="startInterval()" style="margin-left: -92;margin-top: 25;"> Continue </button></b></h3>
</div>
</div>
</body>
关于javascript - 如果按 Interval(Continue btn) 两次或更多次,则 AnglularJS Interval 不会暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38628243/
我是 angularjs 新手,我想从另一个 html 文件加载新内容,我使用下面的代码,但我不知道为什么会出现此错误 Error: [$injector:modulerr] http://error
我有一个使用 AngularJS 间隔功能连续运行的毫秒时间。我创建了一个暂停 和继续 按钮。当点击 Pause 按钮时,它将触发 AngularJS 的 Interval.cancel 功能,如果我
我是一名优秀的程序员,十分优秀!