View Full Version : setInterval does not work as it is supposed to
JAVAEOC
11-29-2003, 03:59 PM
<html>
<body>
<style>
.Clock{
border: 1px solid #154160;
width:250;
font-size: 35;
color: #154160;
filter: glow(color=#F1EED5, strength=1);
text-align: center;
padding: 10px;
}
body {
background: #154160;
color: #000000;
scrollbar-face-color: #154160;
scrollbar-track-color: #154160;
scrollbar-arrow-color: #000000;
scrollbar-highlight-color: #000000;
scrollbar-3dlight-color: #154160;
scrollbar-shadow-color: #000000;
scrollbar-darkshadow-color: #154160;
}
</style>
<script type="text/javascript">
var a=0;
var interval=0;
function glow(){
a=0
interval=setInterval("makeglow()",50)
}
function back(){
clearInterval(interval)
document.getElementById('Clock').style.filter="glow(color=#F1EED5, strength=1)"
}
function makeglow(){
a++
if(a<=6){document.getElementById('Clock').style.filter="glow(color=#F1EED5, strength=" + a + ")"
}}
var interval = setInterval("Timer()", 1);
var milli=0;
var sec=0;
var min=0;
var hours=0;
function Timer(){
Clock.innerHTML=" - - - - - - - - - - - -<BR>";
if(hours<10){Clock.innerHTML+="0"};
Clock.innerHTML+=(hours);
Clock.innerHTML+=(":")
if(min<10){Clock.innerHTML+="0"};
Clock.innerHTML+=(min);
Clock.innerHTML+=(":");
if(sec<10){Clock.innerHTML+="0"};
Clock.innerHTML+=(sec);
Clock.innerHTML+=(":");
if(milli<100){Clock.innerHTML+="0"};
if(milli<10){Clock.innerHTML+="0"};
Clock.innerHTML+=(milli);
Clock.innerHTML+="<BR> - - - - - - - - - - - -";
milli++;
if(milli>999){milli=0; sec++};
if(sec>59){sec=0; min++};
if(min>59){min=0; hours++};
}
</script>
<table padding=2>
<tr><td id="Clock" class="Clock" onmouseover="glow()" onmouseout="back()"></td></tr>
</table>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</body>
</html>
if you copy and past the code above into the notepad and then open it in IE (others wont work) then you should see a stop watch
but it doesnt work....
its too slow. Eventhough i have set it to increase the milliseconds once every millisecond it still is too slow... why :confused:
COBOLdinosaur
11-29-2003, 04:19 PM
The cpu cannot possible keep up with that, unless you are user a Cray to run the client. The glow filter alone is probably enouhg to peg the cpu.
There are enough instructions in the timechange function to choke it on anything less than 100 milliseconds on p4 at 2.6 so you better go back to the drawingboard this is a non-starter from the word go.
JAVAEOC
11-29-2003, 04:22 PM
so you are saying my pc is too slow... right?
JAVAEOC
11-29-2003, 04:30 PM
Weel,,, i just stick to hours:min:sec:NOmilli sec :)
thx
Still won't work exactly. If you tell the CPU to do anything, it's going to take an amount of time, and then if you wait another second then the pause will be more than a second. The time used by setInterval may be negligable, but consider what would happen if the user has many other programs open?
JAVAEOC
11-30-2003, 02:40 AM
well... anyone?//////
how can i make a timer, wich works?
:confused:
liorean
11-30-2003, 03:56 AM
Well, if you examine the setTimeout and setInterval functions, you will see that they operate on the Windows timeframe. This means that the least time you can put in a timeout or interval to make it work without prolonging the actual delays by many times it's specified value, is 55ms on a Win9x system (95, 98, 98SE, ME, CE?) and 10ms on a WinNT system (including 2k, XP, 2k3). Note also that the actual time in the delays is a bit variable and changes much with very small CPU usage changes.
JAVAEOC
11-30-2003, 03:01 PM
ok... thx...
But does anyone know how to do a timer which works?
There's probably a way you can examine the system clock, and update your timer based on that. I wouldn't know, though.
Code Wizard
11-30-2003, 04:27 PM
SetInterval() method -belonging to the window object:
->Can be used to evaluate JS code afer a period of time;
->Can be used for recursive functions(if you want a delay between each interval);
Bad thing is that if you use a function as setInterval's argument,that function may not have any arguments of it's own...
ex:
//--- This works ---
setInterval("myfunction()",5000);
//--- This doesn't ---
setInterval("myfunction(arg)",5000);
liorean
11-30-2003, 04:45 PM
That is easily worked around:function fnCreateClosure( a, b ){
return function(){
/* do something with a and b here */
}
}
var interval = setInterval(
fnCreateClosure( this, that ),
iMilliSeconds );Of course, you can use as many or as few arguments as you like. They will be "saved" in a closure, and thus their value (at the time of the call to fnCreateClosure) will be remembered when the return function is called.
JAVAEOC
11-30-2003, 07:39 PM
but thats not going to help
... my cpu is still going to die :(
i need a timer... which is good and acurate... does not have to be milliseconds only seconds is enough :p
liorean
11-30-2003, 07:51 PM
Use setInterval at tenths of a second or something like that, and compare dates using the Date object.
JAVAEOC
11-30-2003, 08:03 PM
i did... when i try to go into milliseconds it works at half the speed of the real clock :(
and if i only use seconds well... it is off by about one sec for each min :(
its not acurate :mad:
driving me insane....
need a timer....:rolleyes: oh well... never trust technology :eek:
liorean
11-30-2003, 08:16 PM
Don't work based on the interval length, it's not reliable. Instead, work from the Date object, as that takes the time from the system clock.
JAVAEOC
11-30-2003, 08:19 PM
cooll... how would u do this?
JAVAEOC
11-30-2003, 08:23 PM
well... I bet its not good... but in case u have IE 6 you can take a look at my script :) wont work in any of the other webbrowers :p
Basscyst
11-30-2003, 11:20 PM
Well I've never been one to re-invent the wheel, unless it's for learning purposes. Here is a link to brothercakes site containing a very nice little stopwatch script. The issues liorean described are adressed there as well.
http://www.brothercake.com/scripts/stopwatch.php
Hope That Helps,
Basscyst
brothercake
12-01-2003, 12:38 PM
Essentially what I do is query the system time twice. When you press the start button I query the time and save it. Then on every iteration of the Timeout, I query the system time again and subtract the first value from that, and that gives you the elapsed time.
So I'm not incrementing the clock on each iteration, I'm working out the current time and displaying the difference. The timeout doesn't actually run every millisecond, it's probably not even close, but that doesn't matter - you're not relying on the timeout to know how much time has elapsed.
It would work just the same for a live clock - your start point could be body onload.
What I'm not sure about is, given that the length of the timeout doesn't have to be accurate, does it matter what time length you choose. I chose 1 millisecond to mean "as fast as possible", but if the script is doing a lot of work in that iteration, does it make a difference - say for example, we know under given conditions that the timeout is actually going to take 10 milliseconds to process, does it slow the computer down to specify 1 rather than 10?
liorean
12-01-2003, 01:09 PM
Originally posted by brothercake
I chose 1 millisecond to mean "as fast as possible", but if the script is doing a lot of work in that iteration, does it make a difference - say for example, we know under given conditions that the timeout is actually going to take 10 milliseconds to process, does it slow the computer down to specify 1 rather than 10? Yes it does. As I said, anything lower than 55ms on a Win9x machine, or 10ms on a WinNT machine will take at least that much time (55/10), but probably a lot more. It also seems like there are really large winnings in making sure that you have a delay set to a time so long that you don't have any actions still being performed when it executes.
JAVAEOC
12-01-2003, 06:01 PM
Brother cake, I did almost the same thing you did but how do you get the minutes.
I display the seconds(the difference), but then i tell it to add one minute if it has 59 seconds, now it is always one second fast.
And if i wanna go to milli seconds it wont work either.
I told it if(sec=59){min++}
now if i set the interval to 1 millisecond it will add one to minutes every millisecond.!!!!!\
JAVAEOC
12-02-2003, 01:16 AM
i got it figured out now....
milliseconds work even one place finer the brothercakes script and it is much shorter... the function is only 10 lines long :)
brothercake
12-04-2003, 10:07 PM
Ah yes - I wanted milliseconds in two digits, but it's returned in 2 or 3.
Interesting gotcha though - you can add '0' or '00' to numbers so they're always the same number of digits, except milliseconds are returned differently from other units:
352
48
06
rather than
352
48
6
---------------------------------
Thanks for the info liorean - I had a suspicion that might be the case. Lately I try to avoid timeouts faster than 200 milliseconds, for the sake of people on older machines.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.