| Author |
Timer in javascript not working
|
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
Hi all . I am building a project that on clicking a starttestbutton the timer will start and when i click on submit button it will stop
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>
But it dosent seem to start when i click on the starttest button why? Can anybody please help
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
|
Edit your post and use code tags please.
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
|
You are missing quotes around your onclick handler.
|
 |
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
I gave quotes like this but it still doesnt work----------- <input type="button" value=" Start Test ..." id="btnStartTest" onclick="CreateTimer("timer", 30)";/>
I tried to put in code block it dosent take . It says some lines are more than 120 charecters
Anayways can you please show mw the program
|
 |
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
|
|
 |
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
|
Here it is on codes . On the click of button start test it should start the timer. Please help
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1196
|
|
|
|
Swastik
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
Now you have a quote mismatch
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1196
|
|
|
Did it work?
|
 |
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
But when i click on starttest nothing happens, It dosent show the timer
Swastic what am i doing wrong?
|
 |
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
|
no it dosent seem to work the timer dosent start i cant see anything
|
 |
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
yes thanks the timer works now but i have two issues . When i click on the starttest it should show up in another page ie . When i click on starttest button it should redirect me to Questions.jsp remember the page we were working on yesterday and start the timer there . Is there a way to do it. Swastik if you are there please let me know.
I click on start test ..... It redirects me to questions.jsp and starts the timer there
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
|
|
How is a JavaScript variable supposed to run between pages?
|
 |
Padmaksha Mukhopadhyay
Greenhorn
Joined: Nov 27, 2012
Posts: 27
|
|
|
If you look at the website http://www.indiabix.com/online-test/java-programming-test and click on any test you will see that if you click on starttest button there it redirects you to the page where it starts the timer
|
 |
 |
|
|
subject: Timer in javascript not working
|
|
|