Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 = true;
        setTimeout("this.disabled=false",3000);
        postinResp();
    });
});
like image 505
zac1987 Avatar asked Dec 04 '22 22:12

zac1987


1 Answers

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);
like image 159
SLaks Avatar answered Dec 23 '22 14:12

SLaks