tandy
tandy

Reputation: 1948

Delaying my script with setTimeout

What I'm trying to do: Delay my script from appending a particular hash to the URL.

Why I'm doing this: I have a jQuery animation that is (and must) fire before the appending of the URL.

I've read about setTimeout, but I have been unable to get it to work. I was thinking that I could delay after the animation occurs, but I didn't see much documentation on this route. Instead, I'm trying to take the common route of delaying the next line of code in the script, which happens to be the URL-append code. For good measure, I've put the animation and the appending code below.

function animate() {
    $(".class").animate({height: "100%"}, 
    $(".class").addClass("open");
}

animate(); // animate the bar up.

//append the url with the comment number and a hash.
var new_hash ="#"+split_comment_number;



var delay = setTimeout(function() {window.location.hash = new_hash;},1500);

Upvotes: 0

Views: 157

Answers (2)

Dave Bish
Dave Bish

Reputation: 19656

You can supply a callback in your animation function, that will fire as soon as the animation has completed:

like so:

$(".class").animate({height: "100%"}, 1000 /* duration */, function(){ /*Do something*/ });

http://api.jquery.com/animate/

Upvotes: 1

James Montagne
James Montagne

Reputation: 78740

The easiest approach would be to just use the callback of animate. It will fire once the animation completes:

$(".class").animate({height: "100%"}, 5000, function(){
    // DO IT HERE
});

Upvotes: 2

Related Questions