View Full Version : Huge Java Noob! Probably a stupid question, but argg....

11-23-2012, 09:41 AM
Good morning all!

I'm working with an html based quiz/test on logic, and I had a timer working for it but I'm having issues having the formail part of the script to message me the timer results. At the moment it messages me the start and finish time in full GMT Time format which, I can also use but I find it frustrating that I spent so much time trying to get a stopwatch to work in this script that now it won't message me the results of it.

Here are the two parts of the script I'm having issues with:

var c1, c2, c3, ID = null;

function startTimer() {
if (ID != null) {
c1 = 0;
c2 = 0;
c3 = 0;
ID = window.setInterval(run, 100);

function stopTimer() {
ID = null;

function run() {
if (c1 == 60) {
c1 = 0;
if (c2 == 60) {
c2 = 0;

var o1 = (c1 <= 9 ? "0" : "") + c1;
var o2 = (c2 <= 9 ? "0" : "") + c2;
var o3 = (c3 <= 9 ? "0" : "") + c3;
document.forms[0].elements[0].value = o3 + ":" + o2 + ":" + o1;


var UserName = '';
var StartTime = (new Date()).toLocaleString();

var ResultForm = '<html><body><form name="Results" action="http://fp1.formmail.com/cgi-bin/fm192" method="post" enctype="x-www-form-encoded">';
ResultForm += '<input type="hidden" name="_pid" value="******">';
ResultForm += '<input type="hidden" name="_fid" value="******">';
ResultForm += '<input type="hidden" name="recipient" value=""></input>';
ResultForm += '<input type="hidden" name="subject" value="A"></input>';
ResultForm += '<input type="hidden" name="Exercise" value="A"></input>';
ResultForm += '<input type="hidden" name="realname" value=""></input>';
ResultForm += '<input type="hidden" name="Score" value=""></input>';
ResultForm += '<input type="hidden" name="Start_Time" value=""></input>';
ResultForm += '<input type="hidden" name="End_Time" value=""></input>';
ResultForm += '<input type="hidden" name="title" value="Thanks!"></input>';
ResultForm += '<input type="hidden" name="bgcolor" value="#C0C0C0"></input>';
ResultForm += '<input type="hidden" name="text_color" value="#000000"></input>';
ResultForm += '<input type="hidden" name="sort" value="order:realname,Exercise,Score,Start_Time,End_Time"></input>';
ResultForm += '</form></body></html>';

function GetUserName(){
UserName = prompt('UVA ID','');
UserName += '';
if ((UserName.substring(0,4) == 'null')||(UserName.length < 1)){
UserName = prompt('UVA ID','');
UserName += '';
if ((UserName.substring(0,4) == 'null')||(UserName.length < 1)){

function SendResults(Score){
var today = new Date;
var NewName = '' + today.getTime();
var NewWin = window.open('', NewName, 'toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no,,width=400,height=300');

//If user has prevented popups, no way to proceed -- exit
if (NewWin == null){

NewWin.document.Results.Score.value = Score + '%';
NewWin.document.Results.realname.value = UserName;
NewWin.document.Results.End_Time.value = (new Date()).toLocaleString();
NewWin.document.Results.Start_Time.value = StartTime;



<html><form name="frm1">
<input type="text" name="timer1">

Old Pedant
11-23-2012, 09:23 PM
There is a much much easier way to do all that.

First, just put the <form> right on the same page as the quiz. Notice the target= that I added.

<form id="Results" action="http://fp1.formmail.com/cgi-bin/fm192" method="post" target="sendResults">
<input type="hidden" name="_pid" value="******">
<input type="hidden" name="_fid" value="******">

Add an <iframe> to your page, thus:

<iframe name="sendResults" style="height: 1px; width: 1px; visibility: hidden;"></iframe>

And then your code to send the form looks like this:

function SendResults(Score)
var form = document.getElementById("Results"); // named forms are obsolete; use id
form.Score.value = Score + '%';
form.realname.value = UserName;
form.End_Time.value = (new Date()).toLocaleString();
form.Start_Time.value = StartTime;

Now you don't have to depend upon a popup working. So long as the browser supports <iframe>s (and no modern browser does not) it works.


If the submittal of the <form> results in something that you *WANT* the user to see, then you don't want a 100% hidden <iframe>, as I have it there, do you?

Okay, simple enough:

<iframe id="sendResults" name="sendResults" style="height: 400px; width: 400px; display: none;"></iframe>

Notice that now it has an id as well as a name. (That name is needed for the target= in the <form> tag.)

And so, you just tack this line in at the end of the SendResults function:


So that the <iframe> turns visible to display the results of the <form> posting.

Old Pedant
11-23-2012, 09:30 PM
LOL! After doing all that, I realized that all you were really saying was that you want JavaScript to *calculate* the number of milliseconds from start to end, right?


var StartTime = new Date();


function sendResults( )
var EndTime = new Date();
var elapsedTime = EndTime.getTime() - StartTime.getTime(); // this *WILL* be in milliseconds!

form.End_Time.value = EndTime.toLocaleString();
form.Start_Time.value = StartTime.toLocaleString();
form.Elapsed_Time.value = elapsedTime; // add this field to your <form>

Philip M
11-24-2012, 09:11 AM
Huge Java Noob! Probably a stupid question, but argg....

Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!