Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

show time in web application in JSP

Posted on 2014-02-21
1
Medium Priority
?
655 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
[X]
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
1 Comment
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

636 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