jquery delay between javascript functions

强颜欢笑 提交于 2019-12-02 23:18:45

问题


I have multiply functions with parameters simplified as:

    function f1(p1,p2){
        alert('Function one is P1:'+p1+' P2:'+p2);        
    }
    function f2(p1,p2){
        alert('Function two is P1:'+p1+' P2:'+p2);
    }

I need to fire these is a sequence with a delay between. I have however found that jQuery dislikes running functions with parameters. I have tried the .click function.

$.delay(1000).click(f1('One',false)).delay(1000).click(f2('One',false));

But the delay makes the click functions not work...


回答1:


I would just use a simple timeout:

f1("one", false);
setTimeout(function() { f2("one", false); }, 1000);



回答2:


$.delay(1000).click(function(){f1('One',false);}).delay(1000).click(function(){f2('One',false);});

not sure what the click is for though ...




回答3:


if you want to delay a function call then a much simpler way is to use setTimeout().

eg: // calling it in a nested setTimeout for sequential delayed execution setTimeout(function(){

 f1('One',false);
  setTimeout(function(){
    f1('One',false)
  },300)
},300)



回答4:


function fn1()
{
    alert(1);
}
function fn2()
{
    alert(2);
}
var arr=[fn1,fn2];
var len=arr.length;
var time=1000;
for(var k=0;k<len;k++)
{
    (function(k)
    {
        setTimeout(arr[k],time);
    }(k))
    time=time*2;
}

It executes after a delay of 1 second! DEMO



来源:https://stackoverflow.com/questions/11782057/jquery-delay-between-javascript-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!