Jquery 在接下来的x秒内禁用div元素

Jquery 在接下来的x秒内禁用div元素,jquery,Jquery,我有一个div元素,它使用jQuery充当刷新按钮。我想做的是停止用户一直按它。假设我想在接下来的15秒内禁用它。我该怎么做 HTML部分 <div class="am_outter_div"> <div class="header">ΜΕΝΟΥ</div> <div class="button_1">BACK</div> <div class="button_2" id="am_refresh_button">RE

我有一个div元素,它使用jQuery充当刷新按钮。我想做的是停止用户一直按它。假设我想在接下来的15秒内禁用它。我该怎么做

HTML部分

<div class="am_outter_div">    
<div class="header">ΜΕΝΟΥ</div>
<div class="button_1">BACK</div>
<div class="button_2" id="am_refresh_button">REFRESH</div>
<div class="button_3">TOP</div>
<div class="button_4">NEXT</div>
</div><br><br>

<div id="av_refresh_div">Blah blah blah...</div>
var $JQ_ = jQuery.noConflict();
    $JQ_(document).ready(function(){
    $JQ_("#am_refresh_button").click(function(){
    $JQ_("#av_refresh_div").load("index.php #av_refresh_div").fadeOut(1000).fadeIn(1000);});
    });

另外,您可以执行以下操作:

  • 单击div时禁用它
  • 设置超时,这将在x秒后重新激活您的div
  • 试试这个:

    $("#am_refresh_button").click(function(){
        if($(this).attr("disabled") != "disabled")
        {
            $("#av_refresh_div").attr("disabled", "disabled");
            $("#av_refresh_div").load("index.php #av_refresh_div").fadeOut(1000).fadeIn(1000);
        }
    
        setTimeout(function() {
            $("#av_refresh_div").removeAttr("disabled");
        }, 10000); // <-- your time (10 sec atm)
    });
    
    $(“#am#U刷新按钮”)。单击(函数(){
    如果($(this).attr(“已禁用”)!=“已禁用”)
    {
    $(“av#u refresh_div”).attr(“禁用”、“禁用”);
    $(“#av#u refresh_div”).load(“index.php#av#u refresh_div”).fadeOut(1000).fadeIn(1000);
    }
    setTimeout(函数(){
    $(“av刷新分区”).removeAttr(“禁用”);
    
    },10000);//您可以解除单击事件的绑定,然后设置一个超时以在15秒后重新绑定。或者在元素上设置一个变量以指示它是否已启用:

    var $JQ_ = jQuery.noConflict();
    $JQ_("#av_refresh_div").data("isEnabled", true);
    
    $JQ_(document).ready(function(){
        $JQ_("#am_refresh_button").click(function(){
            if ($JQ_("#av_refresh_div").data("isEnabled")) { 
                $JQ_("#av_refresh_div").data("isEnabled", false);             
                $JQ_("#av_refresh_div").load("index.php#av_refresh_div").fadeOut(1000).fadeIn(1000);
                setTimeout(function() {
                    $JQ_("#av_refresh_div").data("isEnabled", true); 
                }, 15000);
            }
        });
    });
    
    但是,您最好使用实际的HTML
    按钮
    s,然后您可以执行相同的操作,但实际禁用/启用它们(因此它们会显示为灰色),这会向用户反馈正在发生的事情。而且它们显然是可点击的(但我想一旦你的div被设置了样式,你可能会想添加/删除一个类,以表明点击性是一种可见的方式)

    您可以添加任意数量的时间。时间通常以毫秒为单位

    您可以这样做:

    var $JQ_ = jQuery.noConflict(), 
        $btbnRefresh = $JQ_("#am_refresh_button");
    
     function refreshBind(){     
    
         $btbnRefresh.on("click",function(){
    
        // after once click unbind click handler
        $JQ_(this).off("click");
    
        //show div fade
        $JQ_("#av_refresh_div").load("index.php #av_refresh_div").fadeOut(1000).fadeIn(1000); 
    
            // after some time re-bind again onclick
            setTimeout(function(){
                $btbnRefresh.on("click",refreshBind);
            },10000);
    
        });     
    }
    
    refreshBind();
    
    在这里演奏小提琴:


    我希望这会有帮助。

    这是在《使用计时器如何》中提出的类似问题?@MadanRam:hmm不,我不这么认为…@Triode:当然,没问题。有什么例子吗?我对jQuery真的很陌生!!!:)我想你应该
    setTimeout
    在那里,否则它会继续启动(并且可能在几次单击事件后提前启用DIV)感谢你的提示,我正在尝试,但有点不对劲…你能举一个JSFIDLE的例子吗?这里是:顺便说一句。我刚刚更新了我的答案(我忘了检查在单击刷新按钮时是否设置了禁用属性)它只工作一个细节…当用户点击它时,十秒钟后必须双击才能工作!!!有什么想法吗?
    var divEnabled = true;
    $JQ_("#am_refresh_button").click(function(){
        if(!divEnabled)
           return;
        divEnabled = false;
        setTimeOut(function(){
            divEnabled = true;
        },yourDelay);
    });
    
    var $JQ_ = jQuery.noConflict(), 
        $btbnRefresh = $JQ_("#am_refresh_button");
    
     function refreshBind(){     
    
         $btbnRefresh.on("click",function(){
    
        // after once click unbind click handler
        $JQ_(this).off("click");
    
        //show div fade
        $JQ_("#av_refresh_div").load("index.php #av_refresh_div").fadeOut(1000).fadeIn(1000); 
    
            // after some time re-bind again onclick
            setTimeout(function(){
                $btbnRefresh.on("click",refreshBind);
            },10000);
    
        });     
    }
    
    refreshBind();