一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

jquery延迟处理和定时执行代码例子

时间:2014-05-09 编辑:简简单单 来源:一聚教程网

1、不应该用sleep,javascript又没有多线程,sleep了就阻塞了。

可以参考这个问题说明js是单线程的原理。

应该换个思路,用setInterval来实现循环。.

 代码如下 复制代码
$("#list div.left").mouseover(function(){
 var item=$(this).attr("id");
 //alert(item);
 t = setTimeout(function(){
 
     $("#"+item).parents("div.oitem").animate({width:"292"},400);
 
     $("#"+item).parents("div.oitem").siblings("div").animate({width:"126"},400);
 },100);
                    }).mouseout(function(){
 clearTimeout(t);
                    });

2、在jquery 1.4中新增了一个叫delay(time);也可以现实延迟执行 time为延迟时间 1000=1s

 代码如下 复制代码

$("button").click(function() {
      $("div.first").slideUp(300).delay(800).fadeIn(400);
      $("div.second").slideUp(300).fadeIn(400);
    });
.delay( duration [, queueName] )

函数可直接用于动画过程中的延时环节,不过它没有callback参数,不能直接用来做定时器。


Added to jQuery in version 1.4, the .delay() method allows us to delay the execution of functions that follow it in the queue. It can be used with the standard effects queue or with a custom queue. Only subsequent events in a queue are delayed; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue.

这是JQuery文档里对.delay()方法的说明,也就是只有后延的事情才会被加入delay()的队列当中。

 

 

热门栏目