前端学习——ionic/AngularJs——获取验证码倒计时按钮
按钮功能为:点击“获取验证码”——按钮不可用-设置倒计时-60秒后重新获取。
代码借鉴于:http://plnkr.co/edit/Swj82MpJSix3a47jZRHP?p=preview
主要实现原理:点击后,设置一个$interval,每一秒更改一次剩余时间,并依赖Angular数据绑定实时显示在页面中。设置一个$timeout,60秒后将按钮初始化到可用状态。
实现代码:
(1)js代码,设置成一个directive以便多次调用。
angular.module('winwin').directive('timerbutton', function($timeout, $interval){
return {
restrict: 'AE',
scope: {
showTimer: '=',
timeout: '='
},
link: function(scope, element, attrs){
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);
}
},
template: '<button on-tap="onClick()" class="button button-calm xgmm-btn" ng-disabled="timer"><span ng-if="showTimer">{{ timerCount }}</span>{{text}}</button>'
};
});
(2)html代码
<timerbutton show-timer="false">获取验证码</timerbutton>
实现效果:
(1)点击之前

(2)点击之后

干货:
<html ng-app="myApp">
<head>
<title>测试_短信验证码</title>
<meta charset="utf-8" />
</head>
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css"/>
<body>
<timerbutton show-timer="false">获取验证码</timerbutton>
</body>
</html>
<script>
angular.module('myApp',[]).directive('timerbutton', function($timeout, $interval){
return {
restrict: 'AE',
scope: {
showTimer: '=',
timeout: '='
},
link: function(scope, element, attrs){
scope.timer = false;
scope.timeout = 60000;
console.log("1-进来了");
scope.timerCount = scope.timeout / 1000;
scope.text = "获取验证码"; scope.onClick = function(){
console.log("2-进来了");
scope.showTimer = true;
scope.timer = true;
scope.text = "秒后重新获取";
var counter = $interval(function(){
scope.timerCount = scope.timerCount - 1;
console.log("3-进来了");
}, 1000); $timeout(function(){
console.log("进来了");
scope.text = "获取验证码";
scope.timer = false;
$interval.cancel(counter);
scope.showTimer = false;
scope.timerCount = scope.timeout / 1000;
}, scope.timeout);
}
},
template: '<button ng-click="onClick()" class="btn btn-primary" ng-disabled="timer"><span ng-if="showTimer">{{ timerCount }}</span>{{text}}</button>'
};
});
</script>
前端学习——ionic/AngularJs——获取验证码倒计时按钮的更多相关文章
- Android中注册获取验证码倒计时按钮
public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...
- Andorid实现点击获取验证码倒计时效果
这篇文章主要介绍了Andorid实现点击获取验证码倒计时效果,这种效果大家经常遇到,想知道如何实现的,请阅读本文 我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取 ...
- iOS 短信验证码倒计时按钮的实现
验证码倒计时按钮的应用是非常普遍的,本文介绍了IOS实现验证码倒计时功能,点击获取验证码,进入时间倒计时,感兴趣的小伙伴们可以参考一下: 实现思路: 创建按钮,添加点击方法: 用NSTimer定时器, ...
- Android 获取验证码倒计时实现
Android 获取验证码倒计时实现 2017年10月24日 09:55:41 FBY展菲 阅读数:2002 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- angular中service封装$http做权限时拦截403等状态及获取验证码倒计时、跨域问题解决
封装$http.做权限时拦截403等状态及获取验证码倒计时: 拦截接口返回状态 var app = angular.module('app'); app.factory('UserIntercepto ...
- clips 前端 js 倒计时 获取验证码的按钮
<a href="javascript:void(0);" onclick="get_captcha()" class="btn btn-def ...
- js点击按钮获取验证码倒计时
//发送验证码倒计时 var clock = ''; var nums = 60; var btn; $("#btnGetVerCode").click(function () { ...
- android学习小例子——验证码倒计时按钮
1.activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- iOS开发之--获取验证码倒计时及闪烁问题解决方案
大家在做验证码的时候一般都会用到倒计时,基本上大家实现的方式都差不多,先贴出一些代码来.. -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t q ...
随机推荐
- JQuery Smart UI
JQuery Smart UI 个人开发的一套使用htm+js的开发框架 SmartUI2.0后续声明 摘要: 感谢很多朋友关注,因为今年一直在另外一个公司做顾问,网络环境管制相当严格,所以一直没有更 ...
- android shape总结 和控制的风格定制
1:shape总结 1):shape文件是放置在drawable文件下的.res/drawable/filename.xml. 2):shape类型:android:shape. 一共同拥有四种:re ...
- ASP.NET MVC应用程序使用异步及存储过程
ASP.NET MVC应用程序使用异步及存储过程 是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译 ...
- 多线程中lock用法的经典实例
多线程中lock用法的经典实例 一.Lock定义 lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.它可以把一段代码定义为互斥段(critical section),互斥段在一 ...
- 解决Github使用Fastly CDN而导致不能加载网页的方法
Github现在基本属于“安全”网站,但 Github使用fastly.net的CDN服务后,其网站在国内经常不能正常加载网页.github.global.ssl.fastly.net的亚洲IP一般为 ...
- linux的运行级别
一.linux共有七种运行级别,内容如下: 级别0:停机状态,系统默认运行级别如果设为0,将不能正常启动: 级别1:单用户模式,只允许root用户对系统进行维护: 级别2:多用户模式,但没有NFS(h ...
- UTF8国际通用为什么还要用GBK?
前序: 一直对编码很痴迷,让数据一目了然不好吗?为什么要去编码,这不是闲的没事,浪费生产力吗? 随着学习的深入,愈加觉得自己无知,世界语言千万种,在互联网的世界里如何表示他们?碰到汉字ASCii就显得 ...
- MD5算法-爬虫学习(五)
在实现爬虫的时候,我们使用Hash结构去存储我们用过的URL的时候,有些URL可能长度很长,为了更加节省空间,我们就要对URL进行压缩,帮它减减肥,这个我们介绍这个MD5算法,可以对URL进行有效的压 ...
- BT是如何下载的
BT协议简介 一.BT下载是怎么来的? 在互联网上下载文件的方式大概有这么几种:FTP.HTTP.BT.eMule(电驴)等, 浏览器会直接支持FTP和HTTP下载,BT和eMule下载一般需要专用的 ...
- [转]iOS: About diagnostic capabilities
Source:http://support.apple.com/kb/HT6331 Each of these diagnostic capabilities requires the user to ...