I have a panel who slide automatically after 13 seconds. I made this with jquery.delay().
The problem appears if I want to manually slide the panel with a click event. It appears that the click event is waiting the end of the delay to effect..
What can I use in place of delay()?
My code :
The delay part:
$('#menu').delay(13000).hide("slide", { direction: "right" }, 400, function () {
if ($('#menu').is(':hidden')) {
$('#trigger').removeClass("triggerdroiteExtended");
$('#trigger').addClass("triggerdroiteCollapsed");
$.cookie('rightfold', 'collapsed', { path: '/' });
}
});
The Click event part:
$('#triggerdroite').click(function () { foo
});
This is a place you’re probably better off using
setTimeoutandclearTimeout. From thedelaydocs:(My emphasis.)
So in this case:
(The outer anonymous function is just a placeholder, I assume your code is already inside some function to prevent creating globals.)
Note I’m using
0as a “no timer running” value.0is not a valid return value fromsetTimeout, so it’s a useful flag.Of course, rather than storing this in a variable as I have above, you could always store it on the
#menuelement itself usingdata.