Use a data attribute to hold the state of the timer. You can use the data attribute to determine if you need to cancel it. You store the timeout id into the data attribute and on the click you check for it.
$('#btnDelete').unbind().click(function(){
var btn = $(this); // reference the button that was clicked
if (btn.data("timer")) { // determine if it was already clicked
window.clearTimeout(btn.data("timer")) // cancel the timeout
btn.data("timer", null); // toggle it to not being used
} else {
var time = setTimeout(function() {
btn.data("timer", null); // reset it if you want
/* your code here */
}, 10000)
btn.data("timer", time); // store the timeout id for next click
}
});
if you want it to restart than do not do the else and there is no need to set the data attribute to null since you will overwrite it.
$('#btnDelete').unbind().click(function(){
var btn = $(this);
if (btn.data("timer")) {
window.clearTimeout(btn.data("timer"))
}
var time = setTimeout(function() {
btn.data("timer", null); // reset it if you want
/* your code here */
}, 10000)
btn.data("timer", time);
});
Example running on multiple buttons
$('.btn-delete').unbind().click(function(){
var btn = $(this); // reference the button that was clicked
if (btn.data("timer")) { // determine if it was already clicked
window.clearTimeout(btn.data("timer")) // cancel the timeout
btn.data("timer", null); // toggle it to not being used
} else {
var time = setTimeout(function() {
btn.data("timer", null); // reset it if you want
btn.text((new Date()).toLocaleString())
}, 3000)
btn.data("timer", time); // store the timeout id for next click
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn-delete">Delete 1</button>
<button class="btn-delete">Delete 2</button>
<button class="btn-delete">Delete 3</button>
<button class="btn-delete">Delete 4</button>