简体   繁体   English

点击后如何取消触发

[英]How to cancel trigger after click

On my website, I have a closed accordion menu (menu-1). 在我的网站上,我有一个封闭的手风琴菜单(菜单1)。 If the user doesn't click on the menu for the first 5 seconds of being on the site, the menu drops down automatically using this js code: 如果用户在进入网站的前5秒钟没有单击菜单,则使用以下js代码自动下拉菜单:

setTimeout(function () {
    $('#menu-1').trigger('click');
}, 5000);

My problem is when the user doesn't wait 5 seconds, and clicks on the menu before the trigger, the trigger still goes off and ends up closing the menu. 我的问题是,当用户不等5秒钟,并且在触发器之前单击菜单时,触发器仍然关闭并最终关闭菜单。 How do make it so that the trigger is canceled if the user clicks on the menu before its triggered? 如果用户在触发菜单之前单击菜单,如何使触发被取消?

You need to store the timeout ID in a variable, so you can later cancel it if the user clicks on the menu. 您需要将超时ID存储在一个变量中,以便以后在用户单击菜单时将其取消。 Like this... 像这样...

var timeout_id = setTimeout(function () {
    $('#menu-1').trigger('click');
}, 5000);

$('#menu-1').click(function() {
    if (timeout_id) {
        clearTimeout(timeout_id);
        timeout_id = 0;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM