When someone clicks my flags from a long list of flags, I want to show the number of selected flags in a small popup. My problem is that the small pop-up element should disappear 5 seconds after the last click, which is normal for one click, but if I quickly check 5 boxes, the timer will still be set in the first field, as a result of which the popup element disappears too fast.
As you can see in my function, I tried to use the clearTimeout(timeoutName)
function, but experienced some problems with its application. The console log states that clearTimeout(timeoutName)
undefined, which I can understand: setTimeout is not running yet.
How can I check if a timer exists before I clear it? Or is this really not the best method? When the checkbox is selected (this function works), a timer may be started, but sometimes it cannot be.
$('.name_boxes').live('click', function() { var checked_count = $('.name_boxes:checked').length; // other stuff clearTimeout(hide_checked_count_timer); // if exists???????? $(".checked_count").hide(); $(".checked_count").text(checked_count+" names selected"); $(".checked_count").show(); hide_checked_count_timer = setTimeout(function() { $(".checked_count").hide(); },5000); });
Any help gratefully received ...
source share