找到你要的答案

Q:Javascript delay after click, set timeout not working

Q:JavaScript的延迟后单击,设置超时工作

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'

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.

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

预先感谢帮助。我是新来的JavaScript,我认为我做的一些基本错误。我想“toggleclass之间阶级。FA和阶级。足总酒吧发次采取1秒我点击类的后ubermenu切换的响应。

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.

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

Be carefull with object "this" inside a setTimeout or setInterval function, because maybe could not be the object that you expect, try 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);
    }); 
});

“这是对象在setTimeout和setInterval函数小心,因为可能不是你所期望的对象,试试这个:

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);
    }); 
});
answer2: 回答2:

Try utilizing .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/

尝试使用。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/

answer3: 回答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);
    });
});

试试这个

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);
    });
});
javascript  jquery  html