如何使用jQuery禁用双击或超时点击? [英] How to disable double clicks or timeout clicks with jQuery?

查看:235
本文介绍了如何使用jQuery禁用双击或超时点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个锚点,它使用jQuery .click事件来启动一些函数。

I have an anchor which is using jQuery .click event to start a few functions.

jQuery(document).ready(function(){  
    jQuery('a.slide_circ').click(function(){ 
        do_all_functions();
        return false; 
    });
});

这是代码简写..
它的工作,因为它需要,每次点击on anchor class slide_circ start do_all_functions。

This is the code in short.. Its working as it needs to, every click on anchor class slide_circ start do_all_functions.

但我有一个问题。如果用户双击它们运行2次或更多的功能...
和想法怎么去地狱。

But I have a problem there. If user make double clicks they run 2 times or more the functions... And the how idea go to hell.

我想我需要禁用双击某种方式或设置超时我的功能,所以它不运行每次。

I think that I need to disable the double click some way or to set timeout on my function so its not running each time. Can anyone help me ?

推荐答案

使用 .data 方法将状态分配给元素。然后,使用 setTimeout 重置状态:

jQuery(document).ready(function() {
    jQuery('a.slide_circ').click(function() { 
        var $this = jQuery(this);
        if ($this.data('activated')) return false;  // Pending, return

        $this.data('activated', true);
        setTimeout(function() {
            $this.data('activated', false)
        }, 500); // Freeze for 500ms

        do_all_functions();
        return false; 
    });
});

这篇关于如何使用jQuery禁用双击或超时点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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