Jquery delay between javascript functions

I have multiplied functions with simplified parameters like:

    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 run this sequence with a delay in between. However, I found that jQuery doesn't like working with parameters. I have tried the .click function.

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

      

But the delay makes it impossible for clicks to work ...

-1


source to share


4 answers


I would just use a simple timeout:



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

      

+9


source


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

      



not sure what the click is though ...

0


source


if you want to defer a function call, it is much easier to use setTimeout ().

for example: // call it in a nested setTimeout for sequential delayed execution SetTimeout (function () {

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

      

0


source


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;
}

      

Executed after 1 second delay! DEMO

0


source







All Articles