Archive

Posts Tagged ‘setTimeout’

jquery效果中加入setTimeout

May 30th, 2009

在jquery效果中,我们可能经常需要在各个效果之间加入一个时间等待。如某个div显示出来,隔几秒之后再隐藏。

jquery官方网站中有一个非常好的例子。

在你的js文件中添加如下代码:

$.fn.wait = function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

具体用法是,.wait( [time], [type] )
第一个参数是时间,默认是1000毫秒。
第二个参数是类型,默认是fx即动画效果

    function runIt() {
      $("div").wait()
              .animate({left:'+=200'},2000)
              .wait()
              .animate({left:'-=200'},1500,runIt);
    }
    runIt();

jquery网站的样例:http://docs.jquery.com/Cookbook/wait

Jquery ,