Win a copy of Design for the Mind this week in the Design forum!
    Bookmark Topic Watch Topic
  • New Topic

Countdowntimer in javaScript not working

 
Padmaksha Mukhopadhyay
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Report post to moderator
What exactly are you trying to achieve when the start button is clicked?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question more than once.
 
    Bookmark Topic Watch Topic
  • New Topic