search

Home  >  Q&A  >  body text

angular.js - How to use angular $interval to implement start timing and end timing on a button

My code logic is as follows, I want to implement it:

  1. Click the query button to turn on the timer. The button text changes to Stop Query,

  2. Click to stop query and cancel the schedule

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

But I failed to cancel the timer, please help...

怪我咯怪我咯2775 days ago533

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-05-15 17:13:04

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-15 17:13:04

    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~

    reply
    0
  • Cancelreply