Solved

show time in web application in JSP

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

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 make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

695 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