This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Countdowntimer in javaScript not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply locked New topic
Author

Countdowntimer in javaScript not working

Padmaksha Mukhopadhyay
Greenhorn

Joined: Nov 27, 2012
Posts: 27
Hi I am trying to build a countdowntimer for a project in which on clicking starttest button the timer will countdown but dont seem to get it right can anybody please help
Here is my code


<html>
<body>

<script ="javascript">
var Timer;
var TotalSeconds;
function CreateTimer(TimerID, Time) {
Timer = document.getElementById(TimerID);
TotalSeconds = Time;

UpdateTimer()
window.setTimeout("Tick()", 1000);
}

function Tick() {
if (TotalSeconds <= 0) {
alert("Time's up!")
return;
}

TotalSeconds -= 1;
UpdateTimer()
window.setTimeout("Tick()", 1000);
}
function UpdateTimer() {
var Seconds = TotalSeconds;

var Days = Math.floor(Seconds / 86400);
Seconds -= Days * 86400;

var Hours = Math.floor(Seconds / 3600);
Seconds -= Hours * (3600);

var Minutes = Math.floor(Seconds / 60);
Seconds -= Minutes * (60);


var TimeStr = ((Days > 0) ? Days + " days " : "") + LeadingZero(Hours) + ":" + LeadingZero(Minutes) + ":" + LeadingZero(Seconds)


Timer.innerHTML = TimeStr;
}


function LeadingZero(Time) {

return (Time < 10) ? "0" + Time : + Time;

}
</script>


<html>
<body>


<div align="left">
<p class="ib-green" style="font-size:12px; font-familty: Verdana, sans-serif;"><b>Instruction:</b></p>
<p class="tp-p-instruction">Total number of questions : <b>20</b>.</p>
<p class="tp-p-instruction">Time alloted : <b>30</b> minutes.</p>
<p class="tp-p-instruction">Each question carry 1 mark, no negative marks.</p>
</div>
<input type="button" value=" Start Test ..." id="btnStartTest" onclick=CreateTimer("timer", 30);/>
</div>

</body>
</html>
</body>
<html>

Please
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18135
    
  39

Padmaksha Mukhopadhyay wrote:Please if anyone knows javascript help


Well, it looks like you already know that Java and Javascript are two different languages ... anyway, let me move this topic to the JS forum for you.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

What exactly are you trying to achieve when the start button is clicked?


Swastik
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

Please do not post the same question more than once.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Countdowntimer in javaScript not working
 
Similar Threads
Problems in Javascript Timer
document[].src returning null
Calculating download time
Countdown Timer or JavaScript replacing HTML
how to create timer in jsp?