火灾连续发挥作用,每次之间有延迟

时间:2012-04-17 01:33:27

标签: javascript jquery function settimeout

我在这样的类中有很多不同的函数:

var helper = {    

     h1 : function(){
           //does  stuff        
    },

     h2 : function(){   
         //does  stuff
    },

    ...

}

我可以通过以下方式执行我需要的东西:

$('#helper').click(function(){
    helper.h4();
 // then delay then execute h5(), delay h6(), etc... 
     });

我怎么能拥有它,所以函数连续执行,每个函数之间定义delay。我不确定如何做到这一点,但我怀疑我需要使用queuedequeuedelaysetTimeout的某些内容?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

setTimeout(helper.h4, 0);
setTimeout(helper.h5, 1000);
setTimeout(helper.h6, 2000);
…

请注意,每次超时都会立即开始,因此延迟来自同一时刻(或多或少)。您可能希望从调用中捕获返回的值,以便您可以取消因任何原因尚未调用的超时。

相关问题