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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Can setInterval stop itself?

    Hi,

    I'm having some issues in a complex Ajax script and it looks like the repeated calls made by a setInterval stop at some point for no known reason. I'm not calling clearInterval, and there seems to be no fatal javascript error, as other javascript code continues executing and responding to user's actions.

    Is there anything that may stop setInterval from calling the given function periodically? Is there anything apart from clearInterval that may remove the interval?

    Thanks a lot for any hints.

  • #2
    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
    Sure.
    Code:
    var interval = setInterval(function() {
        blablabla
    
        if (somecondition)
            clearInterval(interval);
    }, 1000);

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    are you setting the setInterval to a variable? ie:
    var int=setInterval("something()",1000);

    if so are you setting it as a global variable or a local variable?

    if setting it inside a function (local) then the variable looses its value when the function ends.

    if you do

    var int=setInterval("something()",1000);

    the variable is cleared when the function ends, which is the same as clearing the interval. or setting the variable to nothing.



    if you were to do something like so:

    <script type=text/javascript>
    var int='';
    function your function(){
    int=setInterval("something()",1000);
    </script>


    it should work, as the variable int is actually a global variable which will retain its value no matter what, unless you specifically reset its value or the page is refreshed.

    }
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.


  •  

    Posting Permissions

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