File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Timer in javascript not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Timer in javascript not working" Watch "Timer in javascript not working" New topic
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: 15376
    
    6
Edit your post and use code tags please.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6



Swastik
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Now you have a quote mismatch
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1478
    
    6

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: 15376
    
    6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer in javascript not working