最近在做angularjs 项目,这是写的一个60秒倒计时, angularjs 与jq不同, 不是使用dom节点操作,而是数据操作,写倒计时,最好是使用$timeout与$interval ,不要使用settimeout与setinterval 。$timeout与$interval 可使绑定数据直接更新。
html
<a href="javascript:" rel="external nofollow" ng-click="sendphonecode(reg_phone)" ng-class="paraclass" ng-bind="paracont">获取验证码</a>
angularjs
angular.module('controllers', []).controller('registerctrl', function($scope,$interval){ $scope.paracont = "获取验证码"; $scope.paraclass = "but_null"; $scope.paraevent = true; var second = 60, timepromise = undefined; timepromise = $interval(function(){ if(second<=0){ $interval.cancel(timepromise); timepromise = undefined; second = 60; $scope.paracont = "重发验证码"; $scope.paraclass = "but_null"; $scope.paraevent = true; }else{ $scope.paracont = second + "秒后可重发"; $scope.paraclass = "not but_null"; second--; } },1000,100); });
以上所述是小编给大家介绍的angularjs 验证码60秒倒计时功能的实现,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
解决三元运算符 报错“SyntaxError: can''t assign to conditional expression”
解决angular 使用原生拖拽页面卡顿及表单控件输入延迟问题
使用 Angular RouteReuseStrategy 缓存(路由)组件的实例代码
分享Angular http interceptors 拦截器使用(推荐)
网友评论