user3713959
user3713959

Reputation: 101

delay before running javascript code

I want to run a piece of code after first completing jquery hide function but the code run before waiting to completely hide the elements.

This is the code:

$('p').hide(1000);
$('.menu-link').each(function(n, e){

   var len = $(e).next('ul').find('a').length;
   $(e).find('div').text(len);

});

How can I run this code after completing hide() function that is complete in 1000 ms?

Upvotes: 0

Views: 53

Answers (3)

mostafaznv
mostafaznv

Reputation: 988

use this code:

setTimeout(function(){  }, 1500);

or use callback!!

$('element').hide("1000", function() { ... });

Upvotes: 0

Manwal
Manwal

Reputation: 23816

Use Call back to hide event

$('p').hide(1000, function () {
    $('.menu-link').each(function (n, e) {

        var len = $(e).next('ul').find('a').length;
        $(e).find('div').text(len);

    });
});

Demo Callback

Upvotes: 3

xdazz
xdazz

Reputation: 160833

Write as callback:

$('p').hide(1000, function() {
  $('.menu-link').each(function(n, e){
    var len = $(e).next('ul').find('a').length;
    $(e).find('div').text(len);
  });
});

Upvotes: 0

Related Questions