单击后Javascript延迟,设置超时不起作用

时间:2021-07-13 04:35:09

Thanks in advance for helping. I am new to JavaScript so i think i'm doing something basic incorrectly. I would like 'toggleclass' between class '.fa' and class '.fa-bars fa-times' to take 1 second after i click on class '.ubermenu-responsive-toggle'

在此先感谢您的帮助。我是JavaScript的新手,所以我觉得我做的事情基本不正确。我希望在'。''和'。fa-bars fa-times'类之间'toggleclass'在我点击类'.ubermenu-responsive-toggle'之后需要1秒钟

The toggle between '.fa' and '.fa-bar fa times' after clicking on '.ubermenu-responsive-toggle' works. I just can't get the timeset delay down. I keep getting JavaScript errors in Chrome's inspector. I will put my best guess below. I'm sure this is something simple but, like I said, JavaScript is new territory for me.
Thanks again for your help.

单击“.ubermenu-responsive-toggle”后,“。fa”和“.fa-bar fa times”之间的切换有效。我只是无法将时间延迟降低。我一直在Chrome的检查员中收到JavaScript错误。我将在下面做出最好的猜测。我确信这很简单,但正如我所说,JavaScript对我来说是一个新的领域。再次感谢你的帮助。

jQuery(document).ready(function($) {
   $( '.ubermenu-responsive-toggle' ).on( 'click touchend' , setTimeout(function(){
      jQuery( this ).find( '.fa' ).toggleClass( 'fa-bars fa-times' );
   }, 1000));
});

3 个解决方案

#1


Be carefull with object "this" inside a setTimeout or setInterval function, because maybe could not be the object that you expect, try this:

在setTimeout或setInterval函数中注意对象“this”,因为可能不是你期望的对象,试试这个:

jQuery(document).ready(function($) { 
    $( '.ubermenu-responsive-toggle' ).on( 'click touchend' , function() {
        var $myToggles = $(this).find( '.fa' );

        setTimeout(function() {
             $myToggles.toggleClass('fa-bars fa-times');
        }, 1000);
    }); 
});

#2


Try utilizing .delay() , .queue()

尝试使用.delay(),.queue()

jQuery(document).ready(function($) {
  $(".ubermenu-responsive-toggle").on("click touchend", function(e) {
    jQuery(this).delay(1000, "toggle").queue("toggle", function() {
      jQuery(this).find(".fa").toggleClass("fa-bars fa-times");
    }).dequeue("toggle");
  });
});

jsfiddle http://jsfiddle.net/pLv0n1w4/1/

#3


Try this

jQuery(document).ready(function ($) {
    $('.ubermenu-responsive-toggle').on('click touchend', function () {
        var that=jQuery(this);
        setTimeout(function () {
            that.find('.fa').toggleClass('fa-bars fa-times');
        }, 1000);
    });
});

#1


Be carefull with object "this" inside a setTimeout or setInterval function, because maybe could not be the object that you expect, try this:

在setTimeout或setInterval函数中注意对象“this”,因为可能不是你期望的对象,试试这个:

jQuery(document).ready(function($) { 
    $( '.ubermenu-responsive-toggle' ).on( 'click touchend' , function() {
        var $myToggles = $(this).find( '.fa' );

        setTimeout(function() {
             $myToggles.toggleClass('fa-bars fa-times');
        }, 1000);
    }); 
});

#2


Try utilizing .delay() , .queue()

尝试使用.delay(),.queue()

jQuery(document).ready(function($) {
  $(".ubermenu-responsive-toggle").on("click touchend", function(e) {
    jQuery(this).delay(1000, "toggle").queue("toggle", function() {
      jQuery(this).find(".fa").toggleClass("fa-bars fa-times");
    }).dequeue("toggle");
  });
});

jsfiddle http://jsfiddle.net/pLv0n1w4/1/

#3


Try this

jQuery(document).ready(function ($) {
    $('.ubermenu-responsive-toggle').on('click touchend', function () {
        var that=jQuery(this);
        setTimeout(function () {
            that.find('.fa').toggleClass('fa-bars fa-times');
        }, 1000);
    });
});