Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

show time in web application in JSP

Posted on 2014-02-21
1
635 Views
Last Modified: 2014-03-01
hi,

In a puzzle application,I need to show timer which will give 30 seconds to the user to answer the question,if the time lapses it goes to next question.

time will start from 30,29,28....5,4,3,2,1,0

How to show the timer in the html/jsp page?
0
Comment
Question by:Rocking
1 Comment
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39878878
How about this

// Script (c) 2011/2014 mplungjan
function interval(duration, fn, delay){
    this.timer = null;
    this.duration = duration;
    this.fn = fn;
    this.start(delay);
}
interval.prototype.start = function(delay){
    if (this.timer) {return;}
    var self=this;
    this.timer = setTimeout(function(){ self.run(); }, delay||0);
};
interval.prototype.run = function(called){
    var self = this,
        nextTick = called ? this.duration - (new Date - called) : 0;
    
    this.timer = setTimeout(function(){
        self.run(new Date);
        self.fn();
    }, nextTick<0 ? 0 : nextTick);
};
interval.prototype.stop = function(){
    clearTimeout(this.timer);
    this.timer = null;
};

function setTimer1() {
  //-------------------------------
    document.getElementById('out1').innerHTML=initialMesssage1;
    var cnt1 = countdownFrom1;
    var timer1 = new interval(1000, function(){
        --cnt1;
        document.getElementById('out1').innerHTML = "In "+ cnt1 + " second"+(cnt1==1?"":"s") +suffix1;
        if (cnt1 < 0) {
            cnt1 = countdownFrom1;
            document.getElementById('out1').innerHTML = restartMessage1;
            timer1.stop();
            timer1.start(delay1*1000);
        }
    }, initialDelay1*1000);
}

Open in new window


using this html

<html>
<head>
<script type="text/javascript" src="countdown.js"></script>
<script type="text/javascript"> 
// only need to change these - for now set as global vars:
var countdownFrom1   = 11;
var initialDelay1    =  3; // number of seconds to delay onload
var delay1           = 5; // number of seconds to delay between
var initialMesssage1 = "This will start in "+initialDelay1+" seconds"; // message to show during initial delay
var restartMessage1  = "This will re-start in "+delay1+" seconds"; // message to show during delay
var suffix1          = " message is gone"; // text is: In nn seconds message is gone

window.onload = function() { // or in $(document).ready 
  setTimer1();
}
</script>
</head>
<body>
<div id="out1">This will start in 3 seconds</div>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question