0

here is a piece of code im working with,

im trying to keep the timer from starting over on page refresh that the user is on. the time and redirect i can worry about later on, but getting localstorage to work is very important.

<script type='text/javascript'>
window.onload=function(){
function countdown( elementName, minutes, seconds )
{
    var element, endTime, hours, mins, msLeft, time;

    function twoDigits( n )
    {
        return (n <= 9 ? "0" + n : n);
    }

    function updateTimer()
    {
        msLeft = endTime - (+new Date);
        if ( msLeft < 1000 ) {
            element.innerHTML = "0:00";
            window.alert("addfgdg");
            // redirect to google after 5 seconds
window.setTimeout(function() {
    window.location.replace = 'http://www.google.com';
}, 5000);

        } else {
            time = new Date( msLeft );
            hours = time.getUTCHours();
            mins = time.getUTCMinutes();
            element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );
            setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );



        }
    }

    element = document.getElementById( elementName );
    endTime = (+new Date) + 0300 * (60*minutes + seconds) + 500;
    updateTimer();
}

countdown( "countdown", 1, 5 );
}//]]> 

</script>

i tried my best doing so but get blank results,

any help appreciated.

Eli
  • 61
  • 8
  • I'm not sure I understand you. On page refresh, you loose everything which you are trying to store some information as cache in local storage. However, your page must detect when it is closing and save those info into local storage and read them back on the next session. Am I close? – Leng Nov 12 '15 at 23:55
  • i would assume so, while the timer is running and lets say someone refreshes the page it just picks up where it left off – Eli Nov 12 '15 at 23:57
  • You could store info in cookies or local database like indexedDb. See this http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event for detecting session close. And this http://stackoverflow.com/questions/3144711/javascript-find-the-time-left-in-a-settimeout to check timer left. On load, you could read back the time left value and setTimeout with it. – Leng Nov 13 '15 at 00:02

0 Answers0