/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienFeb 13.2011 — 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);[/CODE]
×

Success!

Help @ctoz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...