Js implements the countdown function after the verification code is sent

Source: Internet
Author: User

Js implements the countdown function after the verification code is sent

Previously I shared the countdown function with only js. Later I tested the function and found that the countdown was unavailable after the page was refreshed or closed. 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

?

1

<Input id = "second" type = "button" value = "get verification code for free"/>

Js operations on cookies

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

// Add a cookie when sending the verification code

Function addCookie (name, value, expiresHours ){

Var cookieString = name + "=" + escape (value );

// Determine whether to set the expiration time. 0 indicates that the browser is disabled.

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 <arrCookie. length; I ++ ){

Var arr = arrCookie [I]. split ("= ");

If (arr [0] = name ){

Return unescape (arr [1]);

Break;

} Else {

Return "";

Break;

}

}

 

}

Main logic code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

$ (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

}

}

// Use ajax to submit a text message interface to the background.

Function doPostBack (url, backFunc, queryParam ){

$. Ajax ({

Async: false,

Cache: false,

Type: 'post ',

Url: url, // the requested 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 );

}

SetTimeout (function () {settime (obj)}, 1000) // run once every 1000 milliseconds

}

// Verify that 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.