SoFunction
Updated on 2025-02-28

Based on JS, the countdown function after sending SMS verification code is realized (the page is closed and the countdown function is not performed)

Related readings:

Based on jQuery, the countdown function after sending SMS verification code is realized (neglect the page and close it)

The following code is the code that the editor brings to you after sending a SMS verification code to you. The code is simple and easy to understand.

The specific code is as follows:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script src="/jquery/3.1.0/"></script>
<script src="/jquery-cookie/1.4.1/"></script>
<!-- <script src="///jquery-cookie/1.4.1/"></script>-->
</head>
<body>
<input  type="text" value="18518181818"/>
<input  type="button" value="Get verification code for free" />
</body>
<script>
//Add cookies when sending verification codefunction addCookie(name,value,expiresHours){ 
//Judge whether to set the expiration time, 0 means that the browser is invalid when closing the browserif(expiresHours>0){ 
var date=new Date(); 
(()+expiresHours*1000); 
$.cookie(name, escape(value), {expires: date});
}else{
$.cookie(name, escape(value));
}
} 
//Modify the value of the cookiefunction editCookie(name,value,expiresHours){ 
if(expiresHours>0){ 
var date=new Date(); 
(()+expiresHours*1000); //The unit is milliseconds$.cookie(name, escape(value), {expires: date});
} else{
$.cookie(name, escape(value));
}
} 
//Get the value of the cookie based on the namefunction getCookieValue(name){ 
return $.cookie(name);
}
$(function(){
$("#second").click(function (){
sendCode($("#second"));
});
v = getCookieValue("secondsremained");//Get the cookie valueif(v>0){
settime($("#second"));//Start countdown}
})
//Send verification codefunction sendCode(obj){
var phonenum = $("#phonenum").val();
var result = isPhoneNum();
if(result){
// doPostBack('${base}/login/',backFunc1,{"phonenum":phonenum});
addCookie("secondsremained",60,60);//Add cookie record, valid time 60ssettime(obj);//Start countdown}
}
//Start countdownvar countdown;
function settime(obj) { 
countdown=getCookieValue("secondsremained");
if (countdown == 0) { 
("disabled"); 
("Get verification code for free"); 
return;
} else { 
("disabled", true); 
("Resend(" + countdown + ")"); 
countdown--;
editCookie("secondsremained",countdown,countdown+1);
} 
setTimeout(function() { settime(obj) },1000) //Execute every 1000 milliseconds} 
//Check whether the mobile phone number is legalfunction isPhoneNum(){
var phonenum = $("#phonenum").val();
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
if(!(phonenum)){ 
alert('Please enter a valid mobile phone number!  '); 
return false; 
}else{
return true;
}
}
</script>
</html>

The above is the countdown function after sending SMS verification code based on JS introduced by the editor (ignoring the page refresh, the page is closed and the countdown function is not performed). I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!