I have an event called customevent. When this event occurs I need to toggle a class on a div.
This is working however the event sometimes fires multiple times in quick succession. When this happens the class is added and removed again which is not what I want.
$(document).on('customevent', function(event, data) {
$('.class').toggleClass('toggle-class');
});
When the event occurs I need the class to be immediately toggled. However I need the class to not be able to be toggled for 1 second even if the event continues to fire. The following is not working.
var is_blocked;
$(document).on('customevent', function(event, data) {
if (is_blocked !== true) {
$('.class').toggleClass('toggle-class');
is_blocked = true;
setTimeout(function(){
is_blocked = false;
}, 1000);
}
});