Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post is the interval finished?

    Hi,

    I wrote this thing:
    Code:
    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.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    243
    Thanks
    14
    Thanked 2 Times in 2 Posts
    setTimeOut() executes once after a specified delay, meaby usefull here...

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #5
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #7
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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?

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,460
    Thanks
    0
    Thanked 632 Times in 622 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    felgall, I'm not sure I understant what you mean.
    Try this code, and see what happens (checked on Fx2 & IE6):
    Code:
    <!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?

  • #10
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just tried to move the alert line to other places, but nothing do the trick.
    Am I missing something?

  • #11
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    Quote Originally Posted by b_hole View Post
    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.
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #12
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You mean the function doSomething should look like this?
    Code:
    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.

  • #13
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Code:
    function doSomething() {
    	a=a-2;
    	document.getElementById("t1").style.width=a+"px";
    	if (a==35 || a==36) {
    		clearInterval(myint);
    		alert("finished interval");
    	}
    }

  • #14
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 (vars a & myint)?

  • #15
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ideas anyone?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •