Eli
Eli

Reputation: 61

JS Countodown Timer

so i have this piece of code im looking to alter or just add to,

    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 = "countdown's over!";
            } 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) + 1000 * (60*minutes + seconds) + 500;
        updateTimer();
    }

    countdown( "countdown", 1, 5 );
  1. while the timer is going, lets say if the person refreshes the page i dont want the timer to restart i want it to continue going.

  2. i would rather a message popup rather that it be included in the html if that makes any sense.

  3. 5 seconds after the time is up i would like it to redirect to a different page.

im not js savvy sorry for the pain in the neck questions.

Upvotes: 0

Views: 60

Answers (2)

Maciej Kwas
Maciej Kwas

Reputation: 6419

  1. http://brian.io/lawnchair/finding/
  2. it would be in html anyway, but anything like: this
  3. window.location.replace("http://example.com");

Upvotes: 0

vacsora
vacsora

Reputation: 207

  1. use localstorage for the timer so as not to restart at refresh
  2. popup: window.alert("sometext");
  3. redirect: window.location.replace("http://url.com")

Upvotes: 2

Related Questions