用vue写手机验证码的倒计时

源码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0, ser-scalable=no">
		<link rel="stylesheet" type="text/css" href="css/reset.css"/>
		<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
		<script type="text/javascript" src="js/axios.min.js"></script>
		<script type="text/javascript" src="js/vue.js"></script>

		<title></title>
		<style type="text/css">
			html { background: #efefef; }
			p { position: relative;height: 1rem;width: 90%; margin: 0 auto;border-bottom: 1px solid lightgray; }
			p input { outline: none;text-indent: 0.4rem;font-size: 0.3rem;height: 100%;width: 100%;background: rgba(0,0,0,0); }
			p span { display: inline-block;font-size: 0.3rem;color: gray;position: absolute;top: 0.35rem;right: 0.3rem; }
			.login { width: 80%;height: 1rem;line-height: 1rem;text-align: center;background: #D81E06;color: #fff;font-size: 0.3rem;margin: 0.5rem auto 0;border-radius: 0.1rem; }
			i { width: 100%;text-align: center;display: inline-block;color: gray;margin-top: 0.4rem; }
			i strong { color: blue; }
		</style>
	</head>
	<body>
		<div id="app">
		<form id="form1">
		<p style="padding-top: 2rem;">
			<input type="number" value=""  id="phone" name="phone" placeholder="请输入手机号"  v-model="phone" />
		</p>
		<p>
			<input type="number" id="code"  name="code" placeholder="请输入验证码" v-model="code"/>
			<span id="sendSms" @click="sendSms"  v-show="show">获取验证码</span> 
			<span  class="count"  v-show="!show">{{count}} 秒后可重新获取</span>
		</p>
		<div class="login" id="login" @click="login">登录</div>
		<i>点击登录,即表示已阅读并同意<strong>《车友援服务协议》</strong></i>
		</form>
	</div>
	 
	</body>
	
	<script>
		 var vm=new Vue({
			el:"#app",
			data:{
				phone:'',
				code:''
			},
			data(){
	  		return {
	   			show: true,
	   			count: '',
	   			timer: null,
	 		 	}
	 		},
			methods: {
				sendSms:function(){
					axios.get('验证码的后端网址', {
					  params: {
						  phoneNum:this.phone
					  }
					}).then((ret) => {
						if (ret.data.code == 0) {
							alert("验证码已发送到手机上");
							// 续上 作业2!!! 让验证码 形成倒计时
	     				const TIME_COUNT = 120;
	     				if (!this.timer) {
	       				this.count = TIME_COUNT;
	       				this.show = false;
	       				this.timer = setInterval(() => {
	      					 if (this.count > 0 && this.count <= TIME_COUNT) {
	       				  		this.count--;
	        				} else {
	         					this.show = true;
	         					clearInterval(this.timer);
	         					this.timer = null;
	        				}
	       				}, 1000)
	     		 }

						}else{
							alert("发送失败")
						}
					})
				},
				login:function(){
					axios.post('登录的后端网址', {
						phoneNum:this.phone,
						codeNum:this.code
					}).then((ret) => {
						console.log(ret.data);
						if (ret.data.code == 0) {
							// 登录成功, 跳转到首页
							alert("登录成功,调转到 首页");
						}else{
							alert("验证码不对啊!")
						}
					});
				}
			}

		 });
		 
	</script>


	 
 
</html>

 

  

效果图: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值