JSFiddle - React, Tailwind, and code Playground

by Deividas Krupstas

HTML

<span id="counter-wrp">Be the first to know when we launch! We are only giving out 100 coupons and there are only <span id="counter"></span> left</span>

JavaScript

var i = 100;
var counter = document.getElementById('counter');
if(localStorage.counter) {
    i = localStorage.counter;
}
function countDown() {
    if(i > 0) {
        i--;
        console.log(i);
        counter.innerText = i;
        localStorage.counter = i;
        var timeout = Math.floor(Math.random() * (5000 - 1000)) + 1000;
        setTimeout(function(){
            countDown();
        }, timeout);
    } else {
        document.getElementById('counter-wrp').innerText = 'Oh no, you missed out! All of the coupons are gone.'
    }
}
countDown();