gpt4 book ai didi

javascript - 如果按 Interval(Continue btn) 两次或更多次,则 AnglularJS Interval 不会暂停

转载 作者:行者123 更新时间:2023-11-28 01:08:35 28 4
gpt4 key购买 nike

我有一个使用 AngularJS 间隔功能连续运行的毫秒时间。我创建了一个暂停继续 按钮。当点击 Pause 按钮时,它将触发 AngularJS 的 Interval.cancel 功能,如果我点击 continue 按钮,它将触发间隔功能,但问题是当我点击Continue 按钮两次或更多次,然后点击 pause 按钮它不会暂停并且毫秒继续运行但是当 continue 按钮时它会暂停被击中一次。下面是 UI 的屏幕截图。非常感谢您的帮助。

enter image description here

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com