angularjs+ionicWeb前端之路IT修真院-前端

angularjs实现获取验证码倒计时按钮

2017-07-15  本文已影响163人  Hank_谢旱

一、controller中代码

angular.module('controllers')
  .controller('LoginCtrl', function ($scope, $location,$ionicLoading,$rootScope,$interval,$timeout) {
    $scope.timer = false;
    $scope.timeout = 60000;
    $scope.timerCount = $scope.timeout / 1000;
    $scope.text = "获取验证码";
    $scope.onClick = function(){
      $scope.showTimer = true;
      $scope.timer = true;
      $scope.text = "秒后重新获取";
      var counter = $interval(function(){
        $scope.timerCount = $scope.timerCount - 1;
      }, 1000);
      $timeout(function(){
        $scope.text = "获取验证码";
        $scope.timer = false;
        $interval.cancel(counter);
        $scope.showTimer = false;
        $scope.timerCount = $scope.timeout / 1000;
      }, $scope.timeout);
    };
  });

二、html页面中

<button class="yz-btn" ng-click="onClick()" ng-disabled="timer"><span ng-if="showTimer">{{timerCount}}</span>{{text}}</button>

注:
1.class="yz-btn"为button的样式,可自己修改;
2.ng-disabled="timer"控制button是否可以点击;
3.ng-if="showTimer"控制数字显示;
4.ng-click="onClick()"触发效果,文字text默认“获取验证码”,点击之后为“60s后重新获取”。

三、效果图

1、点击前

点击前.png

2、点击后

点击后.png
上一篇 下一篇

猜你喜欢

热点阅读