basic delay on jquery .click function

后端 未结 2 519
走了就别回头了
走了就别回头了 2021-01-04 12:54

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

相关标签:
2条回答
  • 2021-01-04 13:19

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

    $('.masonryRecall').click(function(){
            setTimeout("$('#mainContent').masonry()", 1500);
        });
    
    0 讨论(0)
  • 2021-01-04 13:27

    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));
    
    0 讨论(0)
提交回复
热议问题