开发者

setTimeout("this.disabled=false",3000); is not working

开发者 https://www.devze.com 2023-03-06 17:48 出处:网络
I am trying to prevent duplicated data to database when the user click the submit button multiple times within a very short time (eg double click speed). First I disable the button after one click, th

I am trying to prevent duplicated data to database when the user click the submit button multiple times within a very short time (eg double click speed). First I disable the button after one click, then enable the button again after 3 seconds. I don't know why setTimeout("this.disabled=false",3000); is not working on jquery. Please help me out, below is my codes :

$(function() { 
    $(".btnSendResp").click(function() {
        this.disabled = t开发者_运维知识库rue;
        setTimeout("this.disabled=false",3000);
        postinResp();
    });
});


You have the wrong this.

You need to save a reference to this in a local variable, then pass a function to setTimeout that uses the variable.

For example:

var self = this;
setTimeout(function() { 
    self.disabled = false;
}, 3000);


$(function() { 
    $(".btnSendResp").click(function() {
        var that = this;
        that.disabled = true;
        setTimeout(function(){
            that.disabled=false;
        },3000);
        postinResp();
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消