pds
03-18-2009, 06:07 PM
I have this code below which creates a stopwatch. What happens though is if i set speed (how fast time passes) to a high value like 4, sometimes when I click the record link the click isn't registered. I think this is due to the code execution being stopped by the triggering of the timer() function. Any ways around this?
It's kind of hard to tell what I'm talking about just looking at the code so I have attached the file as a .txt file. Rename it to a .html file.
(Note: I have multiple text boxes because I am trying to time multiple events).
var time = 0;
var speed = 1;
var clock;
var count = 0;
var disptime = "";
var seconds = 0;
timer = function() {
var timelabel = document.getElementById("timer");
//format the seconds string to disptime
var seconds = (time%60) + "";
if(seconds.length == 1) {
seconds = "0" + seconds;
}
disptime = Math.floor(time/60) + ":" + seconds;
//make the label display the time, add a "Record" link that writes the displayed time to a textbox
timelabel.innerHTML = "<p><strong>" + disptime + "</strong> <span class='time'><a href='javascript:record(\"" + disptime + "\")'>Record</a><span></p>";
time++;
//if the number of records is greater than a certain value, stop
if(count >= document.getElementById('number').value) {
timelabel.innerHTML = "";
clearInterval(clock);
}
}
function start() {
timer();
clock = setInterval("timer()", (1000/speed));
}
record = function(dt) {
var textbox = document.getElementById("time" + count);
textbox.value = dt;
count++;
}
It's kind of hard to tell what I'm talking about just looking at the code so I have attached the file as a .txt file. Rename it to a .html file.
(Note: I have multiple text boxes because I am trying to time multiple events).
var time = 0;
var speed = 1;
var clock;
var count = 0;
var disptime = "";
var seconds = 0;
timer = function() {
var timelabel = document.getElementById("timer");
//format the seconds string to disptime
var seconds = (time%60) + "";
if(seconds.length == 1) {
seconds = "0" + seconds;
}
disptime = Math.floor(time/60) + ":" + seconds;
//make the label display the time, add a "Record" link that writes the displayed time to a textbox
timelabel.innerHTML = "<p><strong>" + disptime + "</strong> <span class='time'><a href='javascript:record(\"" + disptime + "\")'>Record</a><span></p>";
time++;
//if the number of records is greater than a certain value, stop
if(count >= document.getElementById('number').value) {
timelabel.innerHTML = "";
clearInterval(clock);
}
}
function start() {
timer();
clock = setInterval("timer()", (1000/speed));
}
record = function(dt) {
var textbox = document.getElementById("time" + count);
textbox.value = dt;
count++;
}