sayman100,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(function() {
$.fn.Timer = function Timer(obj) {
var def = {
from: 5E3,
duration: 5E3,
to: 0,
callback: null,
step: function(now, fx) {
$(fx.elem).html(now | 0)
}
};
var opt = $.extend({}, def, obj);
return this.each(function(indx, el) {
$(el).queue(function() {
el.n = opt.from;
$(el).dequeue()
});
$(el).animate({
n: opt.to
}, {
easing: "linear",
duration: opt.duration,
step: opt.step,
complete: opt.callback
})
})
};
var end = +localStorage.getItem("end") || 0,
duration = 20 * 60 * 1E3;
var num = +localStorage.getItem("num") || 0;
var max = 20;
$("#send_sms").on("click", function(event) {
num++;
localStorage.setItem("num", num);
this.textContent = `Получить код в СМС, осталось ${max - num}`;
if (num < max) return;
var time = (new Date).getTime(),
d = duration;
if (end && end > time) {
event.stopPropagation();
d = end - time;
} else {
localStorage.setItem("end", time + duration);
}
var f = d / 1000 | 0;
var text = this.defaultValue;
$(this).prop({
disabled: true
}).Timer({
step: function(now, fx) {
var a = Math.trunc(now);
var b = ['секунда', 'секунды', 'секунд'];
a = `${a} ${b[1 == a % 10 && 11 != a % 100 ? 0 : 2 <= a % 10 && 4 >= a % 10 && (10 > a % 100 || 20 <= a % 100) ? 1 : 2]}`;
fx.elem.textContent = `Подождите ${a}`;
},
from: f,
duration: d,
to: 0,
callback: function() {
localStorage.setItem("end", "0");
localStorage.setItem("num", "0");
num = 0;
$(this).prop({
disabled: false
}).val(text)
}
})
})
if (num) $("#send_sms").click();
});
</script>
</head>
<body>
<button id="send_sms" name="send_sms" type="button">Получить код в СМС</button>
</body>
</html>
|