Skip to content

Instantly share code, notes, and snippets.

@dannyfinks
Last active October 23, 2016 01:47
Show Gist options
  • Save dannyfinks/5d941b92ea0bec372f7cbe2bea4863cb to your computer and use it in GitHub Desktop.
Save dannyfinks/5d941b92ea0bec372f7cbe2bea4863cb to your computer and use it in GitHub Desktop.
function createCookie(a, b, c) {
if (c) {
var d = new Date;
d.setTime(d.getTime() + c * 60 * 1e3);
var e = "; expires=" + d.toUTCString()
} else var e = "";
document.cookie = a + "=" + b + e + "; path=/"
}
function readCookie(a) {
for (var b = a + "=", c = document.cookie.split(";"), d = 0; d < c.length; d++) {
for (var e = c[d];
" " == e.charAt(0);) e = e.substring(1, e.length);
if (0 == e.indexOf(b)) return e.substring(b.length, e.length)
}
return null
}
function eraseCookie(a) {
createCookie(a, "", -1)
}
function crtmedir(a) {
var b = document.createDocumentFragment(),
c = document.createElement("div");
for (c.innerHTML = a; c.firstChild;) b.appendChild(c.firstChild);
return b
}
function startTimer(start, duration, display) {
var diff, minutes, seconds;
function timer() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (diff <= 0) {
clearInterval(inti);
document.getElementById("countdownhere").innerHTML = "Order reservation ended.";
start = Date.now() + 1000;
}
};
timer();
var inti = setInterval(timer, 1000);
}
var pdm = crtmedir('<div class="countdownholder"><div id="countdownhere" style="display:block;background:#fff5d2;padding:10px 20px;border:1px solid #e3df74;font-size:14px;color:#2c2c2c;font-weight:bold;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px; margin:20px 0px">Your order is reserved for <span id="time"></span> minutes!</div></div>');
if (window.location.href.indexOf('checkout') > -1 && document.location.href.indexOf('thank_you') === -1) {
window.onload = function() {
document.getElementsByClassName('main__header')[0].appendChild(pdm);
var dolzina = 10;
var ten = 60 * dolzina;
var starttime = Date.now();
var xcnt = readCookie('prtcntdwn');
if (xcnt) {
if (starttime < xcnt) {
ten = (xcnt - starttime) / 1000;
} else {
eraseCookie('ptcntdwn');
createCookie('prtcntdwn', Date.now() + (ten * 1000), ten + 1);
}
} else {
createCookie('prtcntdwn', Date.now() + (ten * 1000), ten + 1);
}
display = document.querySelector('#time');
startTimer(starttime, ten, display);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment