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
    Regular Coder
    Join Date
    Aug 2004
    Posts
    280
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to self-cancel window.setinterval ?

    i have a function, which i would like to repeat with setinterval until some conditions are set.

    myinterval = window.setInterval('myfunction();if(condition)window.cancelInterval(how)',tout);

    okay, the question would be how can i pass the variable myinterval in that cancelinterval? or is any other way to get the intiger which leads to interval's canceling?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    var myinterval=setInterval(
        function(){
            myfunction();
            if(cond)
                clearInterval(myinterval);
        },tout);
    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
    Oct 2004
    Posts
    168
    Thanks
    0
    Thanked 5 Times in 5 Posts
    You need to use clearInterval( ) :


    a=0;
    function message()
    {
    a = a + 1;
    alert(a);
    if(a==10){alert("Cleared");window.clearInterval(myInterval);}
    }

    var myInterval = setInterval("message()",2000);

    <edit reason="one-fingered typing">
    You beat me to it lorean
    </edit>


  •  

    Posting Permissions

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