Hi,
Mike was indeed right in terms of the Function#defer. All that defer
does is execute the function later. It doesn't prevent it from being
executed more than once.
If you want to prevent the effect from being fired a second time,
you'll have to check for that condition. There are any number of ways
you can do that. Probably the simplest is a flag on the slide
instances that you sent when fading/appearing them and clear in the
afterFinish callback of the effect.
But just not firing the effect doesn't seem like the right thing.
What if the user clicks Slide A, then B, then C, fairly quickly? That
would mean (for instance) that B may still be appearing when they go
to C. Surely you don't want to just not do the fade on B, because you
do want it to go away.
One way you can do that is to keep track of the current effect and
cancel it when a new one is going to be done, something like this
(untested):
// Somewhere convenient
var cancelPrevEffectOpts = {
beforeStart: function(effect) {
// Cancel any previous effect on this element
if (effect.element.currentEffect) {
effect.element.currentEffect.cancel();
}
// Remember this one
effect.element.currentEffect = effect;
},
afterFinish: function(effect) {
// Since we're done, clear our reference from the element
effect.element.currentEffect = undefined;
}
};
// Then in showSlide, use those opts:
showSlide: function(slide){
$(slideshow.slides[slideshow.currentSlide]).fade
(cancelPrevEffectOpts);
$(slideshow.slides[slide]).appear(cancelPrevEffectOpts);
slideshow.currentSlide = slide;
}
If you don't like having the reference actually on the element, it's
easy enough to adapt the above to use a separate "active effects"
store keyed by the ID of the element:
// Somewhere convenient
var prevEffects = {};
var cancelPrevEffectOpts = {
beforeStart: function(effect) {
// Cancel any previous effect on this element
var id, prevEffect;
id = 'e' + effect.element.identify();
prevEffect = prevEffects[id];
if (prevEffect) {
prevEffect.cancel();
delete prevEffects[id];
}
// Remember this one
prevEffects[id] = effect;
},
afterFinish: function(effect) {
// Since we're done, clear our reference to the effect
var id = 'e' + effect.element.identify();
if (prevEffects[id]) {
delete prevEffects[id];
}
}
};
FWIW,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available
On Jul 15, 5:11 pm, Paul Kim <[email protected]> wrote:
> Hi Mike, I think you are most likely right because Firebug doesn't output
> any errors.
>
> However, defer() does not seem to be doing what I expected it to. When a
> user clicks the buttons very quickly, the fading images might either flicker
> or disappear. I didn't want to queue the fade/appear effects, but instead
> wanted to prevent the fade/appear effects from triggering when the image is
> currently undergoing a fade/appear effect. Do you know if this is even
> possible?
>
> - Paul
>
>
>
> On Wed, Jul 15, 2009 at 7:28 AM, Mike Glen <[email protected]> wrote:
>
> > Paul Kim wrote:
> > > Basically, this is the error I get from firebug when I run
> > > slideshow.showSlide(i).defer():
> > > slideshow.showSlide(i).defer() is undefined
>
> > > How can I defer the showSlide() function so that when users click on
> > > the buttons too quickly, it will wait for the interpreter to idle
> > > before attempting to run the showSlide() function again.
> > > I think I am on the right track in trying to use defer(), instead of
> > > queueing the effect to suppress events being fired too quickly.
> > > However, I don't know whether my use of defer() is correct.
>
> > i think it might need to be slideshow.showSlide.defer(i)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---