I have a simple piece of code which executes on the click of a button, but I need to detect a double click (only one click is required - but I need to alert on two or more clicks). I have a setTimeOut
function to pause for a second then move on - but the code I have, which should clear this function isn't working, it's executing the alert()
but not stopping the setTimeOut
function!
$('.badge').on('click','#badge', function() {
Click = Click + 1
ButtonClick()
if ($.isNumeric($(this).text())) {
$(this).css("background-color","#006400");
BadgeNo = $(this).text()
if (Click == 1) {
var timeOut = setTimeout(function(){
$(this).css('background-color','#568bc1')
$('.badge').hide()
$('.hs').show()
$('#numberplate').text('')
}, 1000);
} else if (Click > 1) {
clearTimeout(timeOut);
alert("Please only select ONE badge number");
CheckBadge();
}
}
});
Am I making any form of schoolboy error here? I just can't understand why it doesn't stop the timeout.
Your var timeOut
is out of scope when unsetting:
var timeOut = false;
$('.badge').on('click','#badge', function() {
Click = Click + 1
ButtonClick()
if ($.isNumeric($(this).text())) {
$(this).css("background-color","#006400");
BadgeNo = $(this).text()
if (Click == 1) {
timeOut = setTimeout(function(){
$(this).css('background-color','#568bc1')
$('.badge').hide()
$('.hs').show()
$('#numberplate').text('')
}, 1000);
} else if (Click > 1) {
if ( timeOut )
clearTimeout(timeOut);
alert("Please only select ONE badge number");
CheckBadge();
}
}
});
slight changes only on the scope of the variable timeOut
and whether its initialized or not.