...

View Full Version : is the interval finished?



b_hole
12-03-2006, 11:26 PM
Hi,

I wrote this thing:

myint=setInterval(doSomething, 2);

And I want to remove a child after the interval is finished.
Is it possible to know that?
If I write the removeChild line right after the setInterval line above, it removes the child during the beginning of the interval.

liorean
12-04-2006, 12:28 AM
The interval never finishes unless you manually clear it - it does the action every 2 ms (on systems that has that fine timer granularity. For windows systems you'll find it at best doing the action every 10ms.)

Kirl
12-04-2006, 08:33 AM
setTimeOut() executes once after a specified delay, meaby usefull here...

brandonH
12-04-2006, 08:43 AM
as kirl mentions above, if you are only wanting to perfrom an action once, and when it is done do something else setTimeout woud be the way to go:

setTimeout("dosomething()",10);
setTimeout('dootherthing()",11);



notice the secont settimeout is one millisecond higher....
setTimout's run after the current function has completed. in this case 10 milliseconds after and 11 milliseconds after.

b_hole
12-04-2006, 11:00 AM
Kirl & brandonH, setTimeout is no good for me here.

liorean, I use clearInterval inside doSomething function (as I should). still - any way to know if it ended?

brandonH
12-04-2006, 01:27 PM
if you are wanting to perform something when the interval is cleard, and you are clearing it within a script, then all you have to do is call what you want to do after it clears right after clearing it. wow that almost doesnt make sense to me, lol.

b_hole
12-04-2006, 03:08 PM
No, as I wrote:

If I write the removeChild line right after the setInterval line above, it removes the child during the beginning of the interval.

To be more specific:

myint=setInterval(doSomething, 2);
alert("finished interval.");

function doSomething(){//the function}
In this case, the remove child executed without waiting to the interval to finish.
Other ideas?

felgall
12-04-2006, 10:09 PM
You have put the finished interval alert just after setting it. It needs to go just after clearing it since when you run the clearInterval is when the interval processing is finished.

b_hole
12-04-2006, 10:38 PM
felgall, I'm not sure I understant what you mean.
Try this code, and see what happens (checked on Fx2 & IE6):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>intervals.</title>
<script type="text/javascript">
onload=function(){
a=document.getElementById("t1").offsetWidth;
myint=setInterval(doSomething, 1);
alert("finished interval.");
}
function doSomething() {
a=a-2;
document.getElementById("t1").style.width=a+"px";
if (a==2 || a==1) clearInterval(myint);
}
</script>
</head>
<body>
<div id="t1" style="border:1px solid red">hello.</div>
</body>
</html>
See when the alert appears?

b_hole
12-05-2006, 04:11 PM
Just tried to move the alert line to other places, but nothing do the trick.
Am I missing something?

brandonH
12-05-2006, 06:49 PM
Kirl & brandonH, setTimeout is no good for me here.

liorean, I use clearInterval inside doSomething function (as I should). still - any way to know if it ended?

put it after the clearInterval, not the setInterval.

b_hole
12-05-2006, 06:57 PM
You mean the function doSomething should look like this?

function doSomething() {
a=a-2;
document.getElementById("t1").style.width=a+"px";
if (a==35 || a==36) clearInterval(myint);
alert("finished interval");
}
If so, that's not good - the alert appears every time until the interval ends.

jkd
12-05-2006, 08:08 PM
function doSomething() {
a=a-2;
document.getElementById("t1").style.width=a+"px";
if (a==35 || a==36) {
clearInterval(myint);
alert("finished interval");
}
}

b_hole
12-05-2006, 11:19 PM
Great! Thank a lot jkd! :)

Now, I have a small question about the code I posted before: I know I must avoid global variables in JS, but how can I avoid it in the example above (http://www.codingforums.com/showpost.php?p=511082&postcount=9) (vars a & myint)?

b_hole
12-06-2006, 07:03 PM
Ideas anyone?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum