This function let’s you wait until a certain javascript event has finished before firing the code inside the function. For example, if you had some function that runs on resizing of the window, you don’t want that function to constantly run every pixel of resizing when someone is making their window smaller. With waitForFinalEvent()
function, the function won’t fire than some specified time after the resizing has finished. Cool, right?!
The Function
// Wait for final event window.waitForFinalEvent = (function () { var timers = {}; return function (callback, ms, uniqueId) { if (!uniqueId) { uniqueId = "Don't call this twice without a uniqueId"; } if (timers[uniqueId]) { clearTimeout(timers[uniqueId]); } timers[uniqueId] = setTimeout(callback, ms); }; })();
Usage
$(window).resize(function () { window.waitForFinalEvent(function () { // do something after window resizing has finished }, 500, "some unique string"); });
Credits: https://gist.github.com/nosp4mSnippets/5846729