locked
Re: timer in asp.net RRS feed

  • Question

  • User-712926555 posted

    Hi, We have plan to develop online test for an interview. I Need to set time 20mins. The timer seconds run on screen ( from decreasing value) after 20 mins, the page is redirect to result page.

    Thanks.

    Friday, March 14, 2014 2:08 PM

Answers

  • User-1404016747 posted

    Heres a snippet which will redirect in X seconds with a time left display added.
    Change the number passed to the function "startCountdown' found in the body tag to the amount of seconds you want to pass before redirecting the user.

    <html>
    <head>
    <title></title>
    <script>
        function startCountdown(timeLeft) {
            var interval = setInterval( countdown, 1000 );
            update();
    
            function countdown() {
                if ( --timeLeft > 0 ) {
                    update();
                } else {
                    clearInterval( interval );
                    update();
                    completed();
                }
            }
    
            function update() {
                hours   = Math.floor( timeLeft / 3600 );
                minutes = Math.floor( ( timeLeft % 3600 ) / 60 );
                seconds = timeLeft % 60;
    
                document.getElementById('time-left').innerHTML = '' + hours + ':' + minutes + ':' + seconds;
            }
    
            function completed() {
                window.location = "http://www.asp.net";
            }
        }
    </script>
    </head>
    <body onload="startCountdown(20);">
    Redirect in <span id="time-left"></span>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 14, 2014 2:30 PM