Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Countdown Timer That Doesn't Reset (https://www.warriorforum.com/programming/1088537-countdown-timer-doesnt-reset.html)

IncomeWitness 2nd July 2015 08:23 PM

Countdown Timer That Doesn't Reset
 
Here's the countdown timer script I'm using on my OTO page. When somebody resets their browser the countdown timer also resets which makes my offer look less credible. How do I make it to where it doesn't reset? Any help would be appreciated. Thanks in advance!

HTML Code:

<script>function startTimer(duration, display) {
var start = Date.now(),
diff,
minutes,
seconds;
function timer() {
// get the number of seconds that have elapsed since // startTimer() was called
diff = duration - (((Date.now() - start) / 1000) | 0);
// does the same job as parseInt truncates the float
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) {
// add one second so that the count down starts at the full duration
// example 05:00 not 04:59
start = Date.now() + 1000;
}
};
// we don't want to wait a full second before the timer starts
timer();
setInterval(timer, 1000);
}
window.onload = function () {
var thirtyMinutes = 60 * 30,
display = document.querySelector('#time');
startTimer(thirtyMinutes, display);
};</script>


2WDHost 3rd July 2015 03:58 PM

Re: Countdown Timer That Doesn't Reset
 
Hi.

You can try to use cookie ( JavaScript Cookies ) or localStorage ( HTML5 Web Storage ) to save the timer value.

IncomeWitness 3rd July 2015 04:35 PM

Re: Countdown Timer That Doesn't Reset
 
Quote:

Originally Posted by 2WDHost (Post 10149650)
Hi.

You can try to use cookie ( JavaScript Cookies ) or localStorage ( HTML5 Web Storage ) to save the timer value.

Thanks! Where would I place that in the coding?

newxxx 5th July 2015 12:46 PM

Re: Countdown Timer That Doesn't Reset
 
i tried placing the script on my website, in the place where i wanted the timer to show, but it doesn't display on my site in either chrome or firefox?

IncomeWitness 9th July 2015 08:59 AM

Re: Countdown Timer That Doesn't Reset
 
You need to add this in the area where you want the timer to display.

HTML Code:

<span id="time"></span>

2WDHost 9th July 2015 04:28 PM

Re: Countdown Timer That Doesn't Reset
 
IncomeWitness, here is the sample of the modified code for you:
HTML Code:

<script>
    function startTimer(duration, display) {
        var start = localStorage.getItem("start"),
                diff,
                minutes,
                seconds;
        if (start === null) {
            start = Date.now();
            localStorage.setItem("start", start);
        }
        function timer() {
            // get the number of seconds that have elapsed since // startTimer() was called
            diff = duration - (((Date.now() - start) / 1000) | 0);
            // does the same job as parseInt truncates the float
            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) {
                // add one second so that the count down starts at the full duration
                // example 05:00 not 04:59
                start = Date.now() + 1000;
            }
        }
        ;
        // we don't want to wait a full second before the timer starts
        timer();
        setInterval(timer, 1000);
    }
    window.onload = function () {
        var thirtyMinutes = 60 * 30,
                display = document.querySelector('#time');
        startTimer(thirtyMinutes, display);
    };
</script>


IncomeWitness 10th July 2015 12:43 PM

Re: Countdown Timer That Doesn't Reset
 
Thank you so much! I asked for help on 4 different websites and you're the first person who was kind enough to assist me with this issue. It works great!


All times are GMT -6. The time now is 03:49 PM.