kalpaitch
kalpaitch

Reputation: 5271

basic delay on jquery .click function

I have the most basic jquery function of them all, but I couldn't find a way in the documentation to trigger the contents of this click function after say 1500 milliseconds:

$('.masonryRecall').click(function(){
  $('#mainContent').masonry();
 });

P.S. just noticed the .delay function jquery 1.4, although, I am using version 1.3. I don't know whether updating this would interfere with any of the other javascript I currently have.

Upvotes: 11

Views: 34617

Answers (2)

David Murdoch
David Murdoch

Reputation: 89342

You should generally stay away from string literals in setTimeout/setInterval. Instead use a closure:

setTimeout(function(){ $('#mainContent').masonry(); }, 1500);`

and even better use it like this (note: the outer closure isn't really necessary):

(function($){
    var timeout=null;
    $('.masonryRecall').click(function(){
        clearTimeout(timeout);
        timeout=setTimeout(function(){$('#mainContent').masonry();}, 1500);
    });
}(jQuery));

Upvotes: 16

Byron Whitlock
Byron Whitlock

Reputation: 53921

You can do it with regular javascript using setTimeout().

$('.masonryRecall').click(function(){
        setTimeout("$('#mainContent').masonry()", 1500);
    });

Upvotes: 17

Related Questions