JQuery + Cookie-based online examination countdown to prevent refresh

Source: Internet
Author: User

JQuery + Cookie-based online examination countdown to prevent refresh

This article mainly introduces the method and example of online examination countdown to prevent refresh Based on jQuery + Cookie. If you need it, refer

JQuery + Cookie-based online examination countdown to prevent refresh

?

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

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

$ (Function (){

Var _ minute = parseInt ("$ {exampaper. paperTime }");

Var _ expiresHours = _ minute * 60*1000;

 

If (! HasSetCookie ()){

AddCookie ("$ {examinee. examineeId}", _ expiresHours, _ expiresHours );

}

Settime ($ ("# remainTime "));

});

Function hasSetCookie (){

Var strCookie = document. cookie;

Var arrCookie = strCookie. split (";");

For (var I = 0; I <arrCookie. length; I ++ ){

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

If (arr [0] = "$ {examinee. examineeId }"){

Return true;

}

};

Return false;

}

// Start countdown

Function settime (remainTime ){

Var _ time = getCookieValue ("871d31bacfd4451484c5f70f8860c2a9 ");

Var _ countdown = parseInt (getCookieValue ("$ {examinee. examineeId}")/1000;

 

If (_ countdown <= 0 ){

Alert ("the exam time is up! ");

EndExam ();

} Else {

Var _ second = _ countdown % 60;

Var _ minute = parseInt (_ countdown/60) % 60;

Var _ hour = parseInt (_ countdown/60)/60 );

 

If (_ hour <10)

_ Hour = "0" + _ hour. toString ();

If (_ second <10)

_ Second = "0" + _ second. toString ();

If (_ minute <10)

_ Minute = "0" + _ minute. toString ();

 

RemainTime.html (_ hour + ":" + _ minute + ":" + _ second );

_ Countdown --;

EditCookie ("$ {examinee. examineeId}", _ countdown * 1000, _ countdown * 1000 );

}

// Execute every 1000 milliseconds

SetTimeout (function (){

Settime (remainTime );

},1000 );

};

 

// Add cookie

Function addCookie (name, value, expiresHours ){

Var cookieString = name + "=" + escape (value); // The escape () function can encode the string so that the string can be read on all computers.

// 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 {

Continue;

};

};

}

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.