.wait(events ,callback)

Description: Wait for a specified event on any of the selected elements before continuing method invocation.

events – string
A string containing one or more event types, such as "click" or "submit" or custom event names.
callback – function(count…), optional
a method that is called once just before continuing the invocation chain.
All current iteration counts/events (from inner-most to outer-most loop) are passed as arguments.

Examples with waiting for an event

Example #15: enable continue button only after making choices

In this example the continue button should get enabled only if for each choice the user has selected some value.

The used parallel .each()..all() loop waits for each line until some radio input is selected. Any order of making choices is allowed.

$('.example .choices li')
  .each().children('input').wait('change').all()
  .$('#continue').fadeIn();
  • Color: red, green, blue
  • Size: XS, S, M, L, XL
  • Textile: wool, hemp, synthetic


Example #19: pause animation cycle on mouseover

There is a animation cycle rotating three images. Once you move the mouse cursor over it, it pauses until the mouse is moved away.

A little helper function checks whether the mouse is over the image. If that's the case a deferred jQuery object waiting for the mouseleave event is returned.
function noHover() {
    return this.is(':hover') ? this.wait('mouseleave') : this
}

The running loop can be written in a single jQuery line.

$('#images > img').hide().repeat().each($).fadeIn($)
    .wait(1000).wait(noHover).fadeOut($);