| 12
 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
 88
 89
 90
 91
 92
 
 | import moment from 'moment';import cookie from '@/helpers/cookie';
 
 const throttle = {
 data() {
 return {
 
 limit: 3,
 
 period: 10,
 
 counter: 0,
 
 attempts: cookie.get('attempts') || [],
 };
 },
 watch: {
 attempts(value) {
 
 if (!value.length) {
 this.setError(null);
 }
 
 if (this.isSuspended) {
 this.countdown();
 }
 },
 },
 created() {
 
 const attempts = cookie.get('attempts');
 this.setAttempts(attempts ? attempts.data : []);
 },
 computed: {
 
 times() {
 return this.attempts.length;
 },
 
 begin() {
 return this.attempts[this.times - this.limit];
 },
 
 end() {
 return this.attempts[this.times - 1];
 },
 
 remaining() {
 return this.period - this.isExpended();
 },
 
 isSuspended() {
 
 if (this.times < this.limit) {
 return false;
 }
 
 return moment.duration(moment(this.end).diff(this.begin)).seconds() < this.period;
 },
 },
 methods: {
 setCounter(counter) {
 this.counter = counter;
 },
 setAttempts(attempts) {
 this.attempts = attempts;
 },
 
 isExpended() {
 return moment.duration(moment().diff(this.end)).seconds();
 },
 
 suspend() {
 const attempts = [...this.attempts, Date.now()];
 cookie.set('attempts', attempts, moment().add(this.period, 's').toDate());
 this.setAttempts(attempts);
 },
 
 countdown() {
 this.setCounter(this.remaining);
 const counter = setInterval(() => {
 this.setCounter(this.counter - 1);
 }, 1000 * 1);
 setTimeout(() => {
 this.setAttempts([]);
 clearInterval(counter);
 }, 1000 * this.remaining);
 },
 },
 };
 
 export default throttle;
 
 |