Solved

show time in web application in JSP

Posted on 2014-02-21
1
621 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now