Closed Thread
Thread Tools Search this Thread
Unread 2nd Jul 2015, 08:23 PM   #1
Active Warrior
 
Join Date: 2015
Posts: 66
Thanks: 4
Thanked 8 Times in 8 Posts
Default
Countdown Timer That Doesn't Reset
Share on: 
fb share twitter share gplus share more share

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>
IncomeWitness is offline  
Unread 3rd Jul 2015, 03:58 PM   #2
Active Warrior
 
2WDHost's Avatar
 
Join Date: 2011
Posts: 96
Thanks: 0
Thanked 26 Times in 25 Posts
Default
Re: Countdown Timer That Doesn't Reset
Share on: 
fb share twitter share gplus share more share

Hi.

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

2WDH.com: Professional cPanel Hosting since 2003
Really helpful support. You're not a number to us! Programming, database and site development help.
No overselling. SSD. Real 99.99%+ uptime. Free 2-week trial: no obligation/credit card required.
2WDHost is offline  
Unread 3rd Jul 2015, 04:35 PM   #3
Active Warrior
 
Join Date: 2015
Posts: 66
Thanks: 4
Thanked 8 Times in 8 Posts
Default
Re: Countdown Timer That Doesn't Reset
Share on: 
fb share twitter share gplus share more share

Originally Posted by 2WDHost View Post

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?
IncomeWitness is offline  
Unread 5th Jul 2015, 12:46 PM   #4
Advanced Warrior
War Room Member
 
Join Date: 2011
Posts: 810
Thanks: 888
Thanked 110 Times in 102 Posts
Default
Re: Countdown Timer That Doesn't Reset
Share on: 
fb share twitter share gplus share more share

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?
newxxx is offline  
Unread 9th Jul 2015, 08:59 AM   #5
Active Warrior
 
Join Date: 2015
Posts: 66
Thanks: 4
Thanked 8 Times in 8 Posts
Default
Re: Countdown Timer That Doesn't Reset
Share on: 
fb share twitter share gplus share more share

You need to add this in the area where you want the timer to display.

HTML Code:
<span id="time"></span>
IncomeWitness is offline  
Unread 9th Jul 2015, 04:28 PM   #6
Active Warrior
 
2WDHost's Avatar
 
Join Date: 2011
Posts: 96
Thanks: 0
Thanked 26 Times in 25 Posts
Default
Re: Countdown Timer That Doesn't Reset
Share on: 
fb share twitter share gplus share more share

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>

2WDH.com: Professional cPanel Hosting since 2003
Really helpful support. You're not a number to us! Programming, database and site development help.
No overselling. SSD. Real 99.99%+ uptime. Free 2-week trial: no obligation/credit card required.
2WDHost is offline  
The Following User Says Thank You to 2WDHost For This Useful Post:
Unread 10th Jul 2015, 12:43 PM   #7
Active Warrior
 
Join Date: 2015
Posts: 66
Thanks: 4
Thanked 8 Times in 8 Posts
Default
Re: Countdown Timer That Doesn't Reset
Share on: 
fb share twitter share gplus share more share

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!
IncomeWitness is offline  
Closed Thread


Bookmarks

Tags
countdown, reset, timer

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 04:54 PM.