Js implements the countdown function after the verification code is sent _ javascript skills

Source: Internet
Author: User
The basic solution of this article is to click and set the button to disabled, then determine whether to set the expiration time based on the cookie, and submit the mobile phone to the background Using ajax to send a text message interface, now I can share with you the function of Countdown with only js. Later I found that after refreshing or closing the webpage, the countdown will be unavailable. I did not find a suitable solution on the Internet, so I wrote it myself. This is an optimized version. After the page is refreshed or re-opened, the countdown will still work.

Note:

The cookie was initially created for 60 seconds. that is to say, if you close the webpage when the countdown is 20. if you open the page after 20 seconds, there will be no countdown display. However, if the countdown is 20, the page will be closed. If you open the page again within 20 seconds, there will be a countdown display.

Html code

 

Js operations on cookies

// Add cookiefunction addCookie (name, value, expiresHours) {var cookieString = name + "=" + escape (value) when sending the verification code; // determine whether to set the expiration time, 0 indicates that if (expiresHours> 0) {var date = new Date (); date. setTime (date. getTime () + expiresHours * 1000); cookieString = cookieString + "; expires =" + date. toUTCString ();} document. cookie = cookieString;} // modify the cookie value function editCookie (name, value, expiresHours) {var cookieString = name + "=" + escape (value ); if (expiresHours> 0) {var date = new Date (); date. setTime (date. getTime () + expiresHours * 1000); // The unit is milliseconds cookieString = cookieString + "; expires =" + date. toGMTString ();} document. cookie = cookieString;} // obtain the cookie value based on the name function getCookieValue (name) {var strCookie = document. cookie; var arrCookie = strCookie. split (";"); for (var I = 0; I

Main logic code

$ (Function () {$ ("# second "). click (function () {sendCode ($ ("# second") ;}); v = getCookieValue ("secondsremained"); // obtain the cookie value if (v> 0) {settime ($ ("# second"); // start countdown}) // send the verification code function sendCode (obj) {var phonenum =$ ("# phonenum "). val (); var result = isPhoneNum (); if (result) {doPostBack ('$ {base}/login/getcode.htm', backFunc1, {"phonenum": phonenum }); addCookie ("secondsremained", 60, 60); // Add cookie record, valid for 60 s Settime (obj); // start countdown} // call the function doPostBack (url, backFunc, queryParam) that submits the mobile phone to the background using ajax. ajax ({async: false, cache: false, type: 'post', url: url, // request action path data: queryParam, error: function () {// processing function for request failure}, success: backFunc});} function backFunc1 (data) {var d = $. parseJSON (data); if (! D. success) {alert (d. msg);} else {// return the verification code alert ("Simulated Verification Code:" + d. msg); $ ("# code "). val (d. msg) ;}// Start countdown var countdown; function settime (obj) {countdown = getCookieValue ("secondsremained"); if (countdown = 0) {obj. removeAttr ("disabled"); obj. val ("Get verification code for free"); return;} else {obj. attr ("disabled", true); obj. val ("resend (" + countdown + ")"); countdown --; editCookie ("secondsremained", countdown, countdown + 1);} setT Imeout (function () {settime (obj)}, 1000) // run once every 1000 milliseconds} // check whether the mobile phone number is valid function isPhoneNum () {var phonenum = $ ("# phonenum "). val (); var myreg =/^ (13 [0-9] {1}) | (15 [0-9] {1 }) | (18 [0-9] {1}) + \ d {8}) $/; if (! Myreg. test (phonenum) {alert ('enter a valid mobile phone number! '); Return false;} else {return true ;}}

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.