PDA

View Full Version : Countdown to input time.



kirtangl
08-09-2008, 11:25 PM
Hi everyone

Just need some help with a countdown script.... the one i have been using is a mix of a few, it has a input fields to enter the HH MM SS, than a onclick function to start the timer... When the time ends, the classname is changed.

Confused yet lol, I attached the file so you can see what i mean.

Now this workes fine but it has a few flaus
1. It dosnt keep time, it eventully ends up a few second up to a few mins off.
2. Im not a good programer thats why there is 1 script per clock lol.
3. i cant think of anything els for now but you will probably find more.

I have been looking at this script:
http://www.dynamicdrive.com/dynamicindex6/dhtmlcount.htm

Just wondering if there is a way to change the script linkes, (or the one i attached) so that it has the same input fields as the one I attached, but instead of 4 clocks, is it poossible to just have one clock, that rolls over, so counts down to 1, than the second than the third and finally the forth, and at the end says somthing like complete...

Thanx very much in advanced for taking the time to read this.
Kirt

Dal
08-11-2008, 03:22 AM
Ive noticed that the setInterval is not the most reliable of things. I wouldnt bake a cake by it but its good for general delay.

kirtangl
08-11-2008, 11:21 PM
Lol yep, and makes things worse when there is alot of loading within the same page, causes it to loose more and more time...

jscheuer1
08-12-2008, 01:35 AM
If you continually reference a fresh Date object with each loop, depending upon the rest of the code, that should keep things on track. If you are relying on the interval to accurately accumulate time though, I can see your problem.

kirtangl
08-13-2008, 10:38 PM
Hi jscheuer1

How would I go about refreshing the data with each loop, or every minute etc, I have had some sucsess but I think I have some problems, the time switches from one set time to the wrong set time, back and forth every second... pretty funny really lol, but somewhat anying.

jscheuer1
08-14-2008, 01:27 AM
I must admit that I've only been following this thread in a general sort of way - with reason. In cases like this, fundamental concepts are sometimes more important than the actual code, which I'm fairly confident you can work up to your satisfaction once you get the idea. But I'll work with you until you get something that works for you.

You should study the Date object, it will prove enlightening. However it has numerous properties, not all of which are applicable to your code.

From what I can tell by skimming this thread, you want to create a reliable timer or reliable timers.

Let's start simple. Say you want to show an alert 20 seconds from now:


<script type="text/javascript">
function timer_1(){
var later = new Date();
later.setSeconds(later.getSeconds() + 20);
var getIt = function(){
var now = new Date().valueOf();
if (now >= later.valueOf()){
clearInterval(getIt.inv);
alert('times up');
}
};
getIt.inv = setInterval(getIt, 100);
};
timer_1();
</script>

kirtangl
08-18-2008, 10:37 PM
Thank you, got that one working, sorry for the late delay, was away for a bit, probably a good time to work on this but didnt have access to a computer.

Dosn't display anything, but with an onlooad funtion after 20 seconds it pops up with a Times up.. seems to do it twice lol but works. :)

EDIT: Decided to read the script, dont need a onload event lol... my bad.