Dinesh G
Dinesh G

Reputation: 1365

How to write delay in jquery?

Actually My Script is

var i=0;
while(i <  folder.length) 
{   

  $("#hide_message").load('/fdc.php?path='+encodeURIComponent(folder[i]) );  

  //Delay After 10 Sec  
  $("#hide_message").load('/member.php?path='+encodeURIComponent(folder[i]) ); 
  //Delay After 10 Sec  

    i++;
}

Is this possible to make delay every line in jquery.

Upvotes: 1

Views: 124

Answers (4)

SE_User
SE_User

Reputation: 370

Please go through the following questions(and it's answers), here in stackoverflow.

delay() and .setTimeout()

jQuery delay and setTimeout

Upvotes: 4

frogatto
frogatto

Reputation: 29285

You may want to do something like this:

var i = 0;
while(i <  folder.length) {   
    var index = i;

    setTimeout(function() {
        $("#hide_message").load('/fdc.php?path=' + encodeURIComponent(folder[index]));  
    }, i * 1000);

    setTimeout(function(){
        $("#hide_message").load('/member.php?path=' + encodeURIComponent(folder[index])); 
    }, (i + 1) * 1000);

    i++;
}

Using this way none of those AJAX loadings coincide.

Note: You shouldn't pass i as an index to folder array. You should instead declare a local variable and pass it. var index = i;. The why would be some side effects of JavaScript closure.

Upvotes: 1

Santanu
Santanu

Reputation: 420

setTimeout(function(){
//your action code                
},1000*10);

try this

Upvotes: 2

cgee
cgee

Reputation: 1887

Of course that is possible.

Take a look into the jquery API document.

https://api.jquery.com/delay/

var i=0;
while(i <  folder.length) 
{   

  $("#hide_message").load('/fdc.php?path='+encodeURIComponent(folder[i]) );  

  //Delay After 10 Sec  
  $("#hide_message").delay(1000).load('/member.php?path='+encodeURIComponent(folder[i]) ); 
  //Delay After 10 Sec  

    i++;
}

You can try this. But I didn't test it!

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

Upvotes: 1

Related Questions