PDA

View Full Version : Can you view all running setIntervals?



Tails
Mar 29th, 2003, 08:00 PM
How can I check whether a setInterval was cleared or not? And can I clear an interval using strings and variables from arrays? Is there a way to view all (running) intervals that in an index similar to document.images[0] for sake of canceling many intervals in a for loop with arrays?

<body>
<img src="z.gif" height="12" width="2" />
<p></p><br />
<form>
<input type="button" value="stop!" onClick="clear(0)" />
</form>
</body>

<script>
A=new Array("grow")

grow=window.setInterval(grow,1)

function grow()
{
document.images[0].length+=2 // grow forever until...
}

function clear(X)
{
clearInterval(A[X])
}
</script>

Graeme Hackston
Mar 29th, 2003, 08:45 PM
I don't know if there's a better way but this is what I do.

Use a div or something on the page and each time the function is fired write something to it with obj.innerHTML +='something'

Tails
Mar 29th, 2003, 08:55 PM
That would work for visual sake, but I want other functions to detect it. I know how setIntervals can build up and run forever, and it would seem appropriate to clear some running ones if I have 5 more running.

Graeme Hackston
Mar 29th, 2003, 09:01 PM
I'm not clear of what you mean. Do you have conditions that stop the intervals? If so the other functions can be fired when they're met.

Also, what is this doing?

document.images[0].length

Tails
Mar 29th, 2003, 09:20 PM
Oops, that was supposed to be width. I made an interval to grow the image and then a button to stop that function name from an array. But it apparently didn't stop apparently, so that won't work.

Graeme Hackston
Mar 29th, 2003, 09:29 PM
You can do something like this. I wouldn't use the same name for a fuction and an interval.

grow2=window.setInterval(grow,1)

function grow()
{
document.images[0].width+=2
if (document.images[0].width >= somemeasure)
{
clearInterval(grow2)
}
}

Tails
Mar 29th, 2003, 09:34 PM
But I don't want to make an IF statement for each interval. I want a common function to turn off each interval. Here's an example page (but the source on this one uses setTimeout juggles) http://XFox_Prower.tirpod.com/future.htm

Graeme Hackston
Mar 29th, 2003, 09:50 PM
that link booted me to this page http://www.megago.com/l/? so I tried it in IE. Got the same page plus popups etc.

If you're doing this with multipule images you can use a for loop. Here's a mock up

for (var i=0;i<document.images.length;i++) {
window['grow' + i] = setInterval("grow("+i+")",1)
}

function grow(num)
{
document.images[num].width+=2
if (document.images[num].width >= somemeasure)
{
clearInterval(window['grow' + num])
}
}

Tails
Mar 29th, 2003, 09:54 PM
window[x] ? Is that for more than one window open or something? This is all on the same page. But if you have a popup blocking script I'd be glad to listen.

Graeme Hackston
Mar 29th, 2003, 10:03 PM
No, it's a reference to a global variable. Try this

<html>
<head>
<title></title>
<script>
a = 1
window['blah' + a] = 2
alert(blah1)
</script>
</head>
<body>
</body>
</html>

Graeme Hackston
Mar 30th, 2003, 01:23 PM
It just sunk in what you ment. Have you got the button going?

stop=false

for (var i=0;i<document.images.length;i++) {
window['grow' + i] = setInterval("grow("+i+")",1)
}

function grow(num)
{
document.images[num].width+=2
if (document.images[num].width >= somemeasure || stop)
{
clearInterval(window['grow' + num])
}
}

--------------

<input type="button" value="stop!" onClick="stop=true" />

Tails
Mar 31st, 2003, 08:30 PM
Well, the main reason I got so curoius about intervals is because of a page I have. Take a look at http://XFox_Prower.tripod.com/future.htm. Notice how the bars grow? This is with a juggling between 2 functions and setTimeout. With strings, I have it as only 2 functions and that's 5 bars. That would work instead having 5 setIntervals and 5 clearIntervals. I also have an equivalent page redone with setIntervals that is larger and I'd like to know from others if it is slower or faster on their machine. I'll have the interval equivalent up in 2 days.

Tails
Apr 2nd, 2003, 09:53 PM
Here they are:
http://XFox_Prower.tripod.com/timeout.htm
http://XFox_Prower.tripod.com/interval.htm

Someone tell me which is faster? The source of them is quite messy, but I hope someone with a somewhat slow machine can see some differences and give me advice.

Graeme Hackston
Apr 2nd, 2003, 10:14 PM
IE6, Moz 1.3, cable.

Speed was a bit faster in Moz than IE but no difference between interval and timeout in either.

Smoothness was of course better in Moz. Ie has that jumpy thing it does with timed visual effects. It's not that noticeable unless you're looking for it.

From best to worst in smoothness

Moz interval
Moz timeout
IE interval
IE timeout

interval seems better overall. I've done similar comparisons and timeout was better. I guess it depends on the situation.

You might gets something useful from this

http://www.webreference.com/dhtml/column28/part-1.html

Tails
Apr 2nd, 2003, 10:24 PM
It is odd how some situations call for the different use of setTimeout and setInterval. For the interval page, I had one interval monitor a long IF statement and once it is met (once all images met their peak width specified), the interval was to clear the other intervals and then clear itself. I tested it with an alert() and it successfully committed suicide lol ^_^.

liorean
Apr 2nd, 2003, 11:17 PM
setTimeout is a CPU and memory hog - and it's leaking in all IE versions so far.

Tails
Apr 2nd, 2003, 11:21 PM
Is that true? I thought setInterval would be the leak hog. Afterall, it has that TSR activity to monitor something. And if you don't clear the interval, how do we know it ended and didn't cause a leak? Is there a good free program to reclaim memory leaks btw?

liorean
Apr 2nd, 2003, 11:36 PM
Oh, but fact is, setInterval is designed in such a way to NOT be a leaky interface (in fact, it was introduced just because of the leakage problems in setTimeout). It WILL be destroyed by clearInterval or destruction of the global object, along with the string and the number passed to it. That was not always the case with setTimeout. Ie's integration into Windows didn't make things better, either.