www.webdeveloper.com
Results 1 to 2 of 2

Thread: stopping a function

  1. #1
    Join Date
    Nov 2005
    Location
    Bundanoon: first village to ban bottled water
    Posts
    218

    stopping a function

    Hi, I've got this hybrid js + jQuery script that actually works: it uses a series of setTimeouts to call a sequence of jQuery color-changes; and then loops them. Callbacks got too complicated for me.

    Can't figure how to stop it: I've looked at clearInterval(), and at jQuery's stop(), but haven't gotten either to work yet. Would appreciate suggestions.

    The stop needs to be a separate function, not integrated into the function itself.
    .myclass {color:#000;}

    function slowPulse() {
    {$(".myclass").animate( {color:"#363300"}, 2000) };
    setTimeout( '{$(".myclass").animate( {color:"#003336"}, 4000) }', 2001);
    setTimeout( '{$(".myclass").animate( {color:"#000"}, 2000) }', 6003);

    setTimeout( function() { setTimeout('slowPulse()', 1000) }, 8004);
    }

    cheers

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,773
    The setTimeout function returns a timeout Id. Save this Id in a variable and when you want to stop the effects, call clearTimeout(), passing it the saved timeout Id:
    Code:
    function callback() {
        alert("I should never get called");
    }
    
    var timerId = setTimeout(callback, 1000);
    
    // stop the timer
    clearTimeout(timerId);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles