JSFiddle - React, Tailwind, and code Playground
HTML
Ваша скидка: <span id="sec">load ...</span>
JavaScript
//http://learn.javascript.ru/cookie
function getCookie(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
function setCookie(name, value, options) {
options = options || {};
var expires = options.expires;
if (typeof expires == "number" && expires) {
var d = new Date();
d.setTime(d.getTime() + expires*1000);
expires = options.expires = d;
}
if (expires && expires.toUTCString) {
options.expires = expires.toUTCString();
}
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for(var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
}
var maxPerc = 25; //максимальная скидка
var cnt = parseFloat(getCookie('timer')) || 0;
var span = document.getElementById('sec');
var timer = setInterval(function(){
cnt += 3.101; //сколько прибавлять в секунду
setCookie('timer', cnt);
span.innerHTML = cnt.toString().slice(0, 5) + '%';
if(cnt >= maxPerc) { //останавливаем таймер при достижении макс.скидки
clearInterval(timer);
}
}, 1000); // 1 секунда