为jQuery扩展wait方法 [jquery1.4之后添加该方法,但不同版本有不一样的写法]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>Wait插件演示</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> <style type="text/css"> div { margin:3px; width:40px; height:40px; position:absolute; left:0px; top:30px; background:green; } </style> </head> <body> <div></div> <script type="text/javascript">//<![CDATA[ $.fn.extend({ wait: function(time, type) { return this.queue(type || "fx", function() { var $this = $(this); setTimeout(function() {$this.dequeue();}, time || 1000); }); } }); (function () { $("div").wait() .animate({left:'+=200'},2000) .wait() .animate({left:'-=200'},1500, arguments.callee); })(); //]]></script> </body> </html>
jQuery.fn.delay
---1.4~1.6.4 function (time, type) { time = jQuery.fx ? jQuery.fx.speeds[time] || time : time; type = type || "fx"; return this.queue(type, function () { var elem = this; setTimeout(function () { jQuery.dequeue(elem, type); }, time); }); } ---1.7.0以后 function (time, type) { time = jQuery.fx ? jQuery.fx.speeds[time] || time : time; type = type || "fx"; return this.queue(type, function (next, hooks) { var timeout = setTimeout(next, time); hooks.stop = function () { clearTimeout(timeout); }; }); }