script Cookbook/wait , :
(function($){
$.fn.runItIf = function(ex, fn, args) {
var self = this;
return $(self).queue(function() {
args = args || [];
if (ex)
fn.apply.(self, args );
$(self).dequeue();
});
};
$.fn.runIt = function(fn, args) {
return $(this).runItIf(true, fn, args);
};
$.fn.wait = function(time, type) {
time = time || 1000;
type = type || "fx";
return this.queue(type, function() {
var self = this;
setTimeout(function() {
$(self).dequeue();
}, time);
});
};
})(jQuery);
An example based on an example in Cookbook / wait :
function runIt() {
var expression = true;
$("div").wait()
.animate({left:'+=200'},2000)
.runIt(function() { } ).wait(10)
.runItIf(expression, function() { } ).wait(10)
.animate({left:'-=200'},1500,runIt);
}
runIt();
source
share