点击后Javascript延迟,设置超时不起作用 [英] Javascript delay after click, set timeout not working

查看:63
本文介绍了点击后Javascript延迟,设置超时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预先感谢您的帮助。我是JavaScript新手,所以我认为我正在做一些基本的错误。我希望类'。fa'和类'之间的'toggleclass'在我点击课程'后,需要花费1秒的时间'> ubermenu-responsive-toggle'

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'

之后在'。fa''。fa-bar fa times'之间切换点击'。ubermenu-responsive-toggle'可以工作。我无法让时间延迟下降。我一直在Chrome的检查器中收到JavaScript错误。我会在下面进行我最好的猜测。我确定这很简单,但正如我所说,JavaScript对我来说是一个新的领域。

再次感谢您的帮助。

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));
});


推荐答案

请小心setTimeout中的对象this setInterval函数,因为可能不可能是你期望的对象,试试这个:

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);
    }); 
});

这篇关于点击后Javascript延迟,设置超时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆