My hack to stop jQuery events from unnecessarily firing.

January 4th, 2013 by admin Leave a reply »

Recently I created a UI where I had 4 text inputs that will change the calculations after you lose focus of each input, each input has an event I am watching the ‘blur’ event, which is just the lostfocus of the input. You can read more about the jQuery blur event here: jQuery Blur

The problem is that jQuery is throwing these events for each of those inputs because my processing code is setting values in the other inputs, which looks like it tosses the events, so I had to write in a little hack, as seen below:

$("#YourInputId").on('blur', function (evt) {
                var $th = $(this);
                if ($th.hasClass('processing'))
                    return;
                $th.addClass('processing');
               //stuff you want to do or process
                $th.removeClass('processing');
            });

This will cause jQuery to stop its processing and wait till its done to fire off, thus not causing any UI weirdness. I hope this is of help to someone, enjoy.

Advertisement

Leave a Reply