473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

timer function

I'm running a timer function that counts down from set time frames.
It has an odd tendency to drop 2 seconds at the beginning but then
work fine the rest of the way. Can anyone suggest what could be
happening here?
function startTimer() {
running = true;
var now = new Date();
start =now.getTime();
var seconds = getSec();
var setTime = 1000 * seconds;
endTime = start + setTime;
showCountDown()
}

function getSec() {
var r = document.frm.radio1;
for (var i=0; i<r.length; i++) {
if (!r[i].checked) continue;
else return r[i].value;
}
return null;
}

function showCountDown() {
var present = new Date();
moment = present.getTime();
if (endTime - moment<= 0) {
stopTimer();
displayTimer.innerHTML = "TIME'S UP!";
showTimeOver();
} else {
var lapse = new Date(moment - start);
var minLapse = lapse.getMinutes();
var secLapse = lapse.getSeconds() + 1;
var timeLapse = minLapse;
timeLapse += ((secLapse < 10) ? ":0" : ":") + secLapse;
document.frm.timeLapse.value = timeLapse;
var delta = new Date(endTime - moment);
var theMin = delta.getMinutes();
var theSec = delta.getSeconds();
theTime = theMin;
theTime += ((theSec < 10) ? ":0" : ":") + theSec;
document.frm.timerDisplay.value = theTime;
displayTimer.innerHTML = theTime;

if (running) {
timerID = setTimeout("showCountDown()",1000)
}
}
}

function stopTimer() {
displayTimer.innerHTML = theTime + " TIMER STOPPED";
clearTimeout(timerID);
running = false;
}

Jul 23 '05 #1
1 2184
JRS: In article <Fv********************@adelphia.com>, dated Tue, 9 Nov
2004 19:20:03, seen in news:comp.lang.javascript, Darren Showers
<dk*******@adelphia.net> posted :
I'm running a timer function that counts down from set time frames.
It has an odd tendency to drop 2 seconds at the beginning but then
work fine the rest of the way. Can anyone suggest what could be
happening here?
if (running) {
timerID = setTimeout("showCountDown()",1000)
}


That gives a delay of 1000 ms, rounded up to the next clock tick; there
will also be the delay in executing the code that precedes it.

You need to calculate the delay to the next second, or say 10% of the
way through it, as for example

setTimeout("Tock()", 1100-D%1000)

where D = new Date().

Read the c.l.j newsgroup FAQ; see below; see js-date0.htm#TaI .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christine | last post by:
I've implemented a countdown timer for a tutorial web page that should give the user 45 minutes to complete the test, only to find that the timer is slowly 'losing' time. On average, it actually...
7
by: J. Hill | last post by:
I have a Windows Service with a timer but the .Tick event is not being fired/called. Don't know what code to include...I enabled and started the timer...I have the exact same code in a Windows...
6
by: Antti Laakso | last post by:
Hi i have function like above Public Sub halytystutkinta() Dim ds As New DataSet ds = dl2.HaeHalytys() Dim onkohal As Int16 onkohal = ds.Tables(0).Rows(0).Item("onkohalytys") halid =...
7
by: Noozer | last post by:
I have a timer on a form. It isn't firing at all. I know that the timer is enabled, and that the interval is low (4000, which should be 4 seconds). To ensure the timer wasn't being inadvertantly...
1
by: jmgro | last post by:
I have spent way too much time trying to solve the following problem: I have a datalist with a timer in the footer template. It works wonderfully except when the user pages back, then forward,...
10
by: Ryan | last post by:
I'm creating a website that monitors the status of servers using My.Computer.Network.Ping. I'm looking for a way to fire off my Ping() function every second or so. I see a Timer control...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
4
by: jwriteclub | last post by:
Hello all, I have a quick question about timers in C#. I have a System.Windows.Forms.Timer timer (the kind you "drag" out of the toolbox. It looks something like this: public class...
7
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
7
by: genesistr | last post by:
Hi, I have timer class and some classes use timer. but when i come back from timer with callback function, i can see this. properties. function Timer(dName, dCallback){ this.timer = null;...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.