简体   繁体   中英

jquery delay between javascript functions

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...

I would just use a simple timeout:

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

虽然不确定点击是为了什么...

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)
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM