angular.js - angular $interval怎么在一个按钮上实现开始定时和结束定时
怪我咯
怪我咯 2017-05-15 17:11:04
0
2
499

我代码逻辑如下,想要实现:

  1. 点击查询按钮,开启定时,按钮文案变成停止查询,

  2. 点击停止查询,取消定时

$scope.submitRequest = function () {
    if ($scope.onlyButton == "查询") {
         $scope.onlyButton = "停止查询";
            $interval(function()
                {
                    //具体方法
                }
                ,5000);
            }
    else{
         setTimeout(function(){
            $scope.onlyButton = "查询";
            $interval.cancel(stop);
            },0)
        };
    }
    

但是没有成功取消定时,求帮忙...

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
Peter_Zhu

淡淡烟草味

LZ’s code can completely make a status mark outside, for example:

var interval;
$scope.startStatus = false;

$scope.submitRequest = function(){

    if( !$scope.startStatus ){
        interval = $interval(...);
    } else {
        $interval.cancel(interval);  
    }
    
    $scope.startStatus = !$scope.startStatus;
    
}

Then use it directly in VIEW like this:

<button ng-bind=" !startStatus ? '查询' : '停止查询' "></button>

I think it’s better to write like this~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template